Linq加入包含声明 [英] Linq Join With Include Statement
问题描述
IQueryable<Employee> emps = CreateObjectSet<Employee>()
.Include(u => u.Departments)
.AsQueryable();
IQueryable<Products> prods = CreateObjectSet<Products>().AsQueryable();
CreateObjectSet是ObjectContext的CreateObjectSetMethod
CreateObjectSet is ObjectContext's CreateObjectSetMethod
return (from emp in emps
join prod in prods
on emp.ProductID equals prod.ProductID
where emp.EmployeeID == 10
select employee).ToList();
问题出在第一行,我使用include语句,将部门与员工合并在一起,因此返回值没有与之相关的部门,因为它们从未被包含.请提出一些建议.
The problem is from the first line, i use the include statement and include departments with the employees the return values does not have departments with as they are never included. Kindly suggest something.
这只是一个演示查询,实际查询非常复杂,所以请不要建议我不要使用join语句,而应该使用简单的include和where子句,这对我的情况不起作用.
This is just a demo query, actual query is far complex, so please don't suggest that i should not go with the join statement, but simple include and where clause, that does not serve me ni my scenario.
谢谢
推荐答案
This is a known issue with include. You could have a look at the following article Include in EF
> var results =
> ((from post in ctx.Posts
> from blog in post.Blogs
> where blog.Owner.EmailAddress == "alexj@microsoft.com"
> select post) as ObjectQuery<Post>).Include("Comments");
如果该解决方案不适合您,您还可以尝试通过对数据进行分组并选择部门作为您类型中的值之一来对其进行修复.
If that solution won't work for you, you can also try to fix it with grouping your data and selecting the departments as one of the values in your type.
然后,EF实体关系修复机制将为您修复"包含.
The EF entity relation fixup mechanism will then 'fix' the include for you.
这篇关于Linq加入包含声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!