简体   繁体   English

LINQ to Entities无法识别ToArray

[英]LINQ to Entities does not recognize ToArray

I'm trying to write a query that will project to a DTO where two of the properties are int arrays. 我正在尝试编写一个将投影到DTO的查询,其中两个属性是int数组。 I'm getting an error because of the ToArray() call in the projection. 由于投影中的ToArray()调用,我收到错误。

teams = context
  .Teams
  .Include("TeamDepartments")
  .Include("TeamEmployees")
  .Select(t => new TeamDto
      {
          sourceSystemId = t.TeamId,
          name = t.Name,
          manager = t.EmployeeIdTeamManager,
          teamLead = t.EmployeeIdTeamLead,
          employees = t.TeamEmployees.Select(te => te.EmployeeId).ToArray(),
          departments = t.TeamDepartments.Select(td => td.DepartmentId).ToArray()
       })
  .ToList();

For employees and departments, which are the two int[ ] properties, how can I get those values? 对于两个int []属性的员工和部门,如何获取这些值? For now, I'm just pulling back the list of teams and then looping over them to create the DTO. 现在,我只是撤回团队列表,然后循环遍历它们以创建DTO。

I've seen other similar questions, but the solutions do not seem to be working for me. 我见过其他类似的问题,但解决方案似乎并不适合我。 I suspect there's an additional step I need to take because I'm traversing a relationship. 我怀疑我需要采取额外措施,因为我正在穿越一段关系。

What you need to do is separate this query into two different steps; 您需要做的是将此查询分为两个不同的步骤; the first will retrieve the correct results, and the second will project the data into your DTO. 第一个将检索正确的结果,第二个将数据投影到您的DTO。 Like this: 像这样:

teams = context
  .Teams
  .Include("TeamDepartments")
  .Include("TeamEmployees")
  .Select(t => new // notice this is an anonymous object
      {
          sourceSystemId = t.TeamId,
          name = t.Name,
          manager = t.EmployeeIdTeamManager,
          teamLead = t.EmployeeIdTeamLead,
          employees = t.TeamEmployees.Select(te => te.EmployeeId),
          departments = t.TeamDepartments.Select(td => td.DepartmentId)
       })
  .ToList() // first run the query on the server without the ToArray calls
  .Select(obj => new TeamDto
      {     // then project the in-memory results onto your DTO.
          sourceSystemId = obj.sourceSystemId,
          name = obj.name,
          manager = obj.manager,
          teamLead = obj.teamLead,
          employees = obj.employees.ToArray(),
          departments = obj.departments.ToArray()
      })
  .ToList();

I believe the problem is that you're attempting to call ToArray within the block that the SQL provider converts to a SQL query. 我相信问题是你试图在SQL提供程序转换为SQL查询的块中调用ToArray Of course, SQL Server has no idea what an array is so that method does not work. 当然,SQL Server不知道数组是什么,所以方法不起作用。 What happens if you remove the ToArray call? 如果您删除ToArray呼叫会怎样? I believe those results will come back as IEnumberables outside of that block you can convert them as needed. 我相信这些结果会在IEnumberables之外返回,你可以根据需要转换它们。 I'm not sure this will work but if you change your TeamDto definition in the following way it may solve the problem. 我不确定这会有效但是如果你用以下方式改变你的TeamDto定义,它可以解决问题。

 // you have now something like
 string[] employees;
 // instead do
 IEnumberable<string> employees;
 // If you want an array add
 string[] _employees;

Then outside of the query to _employees = employees.ToArray(); 然后在查询之外_employees = employees.ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM