实体框架来自多个表的不同记录

时间:2013-08-07 20:18:02

标签: c# entity-framework-4 distinct

我试图通过连接多个表来获得不同的值。有人可以告诉我如何在下面的代码中做同样的事情吗?

请参阅以下示例代码:

var distinctValues = (from a in dataContext.A_Table
                      join b in dataContext.B_Table on a.EmpID equals b.EmpID
                      join c in dataContext.C_Table on b.SomeID equals c.ID
                      where a.IsActive == true
                            && a.ID == id
                      select new NewClass()
                                 {
                                    ID = c.ID,
                                    Name = c.Name
                                 }).ToList();

1 个答案:

答案 0 :(得分:1)

这应该可以在没有很多变化的情况下工作,尽管我会完全用表达式写出来:

var distinctValues = (from a in dataContext.A_Table
                      join b in dataContext.B_Table
                      on a.EmpID equals b.EmpID
                      join c in dataContext.C_Table
                      on b.SomeID equals c.ID
                      where a.IsActive == true
                      && a.ID == id
                      select new NewClass()
                      {
                          ID = c.ID,
                          Name = c.Name
                      }).ToList()
                      .GroupBy(x=>new {ID = x.ID,Name = x.Name})
                      .Select(x=>new {ID = x.Key.ID,Name = x.Key.Name});