【发布时间】:2016-03-15 00:23:51
【问题描述】:
我们有以下数据库结构:
我们如何查询属于特定客户和组的所有数据? (使用 Entity Framework 6 编写的示例会很棒。)
我们可以这样做:
var parentChild1 = dbcontext.Parent1.Include(p => p.Child1).Select(p => p.ClientId = clientId).ToList();
var parentChild2 = dbcontext.Parent2.Include(p => p.Child2).Select(p => p.GroupId = groupId).ToList();
// And the manually join the values in parentChild2 with the objects in parentChild1.
// But there has to be a better way than this.
我在想这样的事情;但不知道如何将 child1(s) 与 child2(s) 连接起来:
void GetData(int clientId, int groupId)
{
var query = (from p1 in dbcontext.Parent1
from c1 in dbcontext.Child1
where p1.ClientId == clientId && c1.Parent1Id == p1.Id
join p2 in dbcontext.Parent2.Where(p2 => p2.GroupId == groupId)
on p1.Id equals p2.Parent1Id into p2groups
join c2 in dbcontext.Child2 on c1.Id equals c2.Child2Id into c2groups
from p2g in p2groups.DefaultIfEmpty()
from c2g in c2groups.DefaultIfEmpty()
select new Parent1
{
Parent2s = p2g,
//Child1s = c2g ??? How to wire up child1s with child2s?
};
}
【问题讨论】:
-
我大概可以给你一个sql答案,有帮助吗?
-
当然,任何帮助将不胜感激。
标签: sql-server c#-4.0 entity-framework-6