无法访问linq中的非分组表的字段

时间:2015-05-26 18:14:30

标签: c# linq entity-framework join

我正在尝试使用LINQ连接几个表并检索数据集。但我的问题是当我对表进行分组时,我无法访问LINQ查询中的非分组表的字段。这是我的示例代码块。请帮忙

\1\2

我想要实现的是这样的

var query = from sb in db.Surfboards
                    join csb in db.CustomerSurfBoards on sb.SurfBoardID equals csb.SurfBoardID
                    join c in db.Customers on csb.CustomerID equals c.CustomerID
                    where c.IsActive
                    group new { sb,csb} by new {sb.ID, csb.ComponentId} into g
                    select new ReportModel()
                    {
                        ReceivedDate = g.First().Name,
                        Number = c.First().Number <------- this cannot be accessed
                    }

1 个答案:

答案 0 :(得分:0)

试试这个:

var query = from sb in db.Surfboards
                    join csb in db.CustomerSurfBoards on sb.SurfBoardID equals csb.SurfBoardID
                    join c in db.Customers on csb.CustomerID equals c.CustomerID
                    where c.IsActive
                    select new {sb.id, c.number, cbs.componentid} into tmp
                    from t in tmp
                    group t by new {t.ID, t.ComponentId} into g
                    select new
                    {
                        g.Key.id, g.Key.componentid, number = g.Select(n=>n.number).OrderByDescending().FirstOrDefault()
                    }