在连接两个表之后访问所有数据并使用linq对它们进行分组

我有两张桌子

TableA aId aValue TableB bId aId bValue 

我想通过aId加入这两个表,然后从那里通过bValue它们进行分组

 var result = from a in db.TableA join b in db.TableB on a.aId equals b.aId group b by b.bValue into x select new {x}; 

我的代码无法识别组后的联接。 换句话说,分组工作,但连接不起作用(或者至少我无法弄清楚如何在连接后访问所有数据)。

groupby之间的表达式创建组元素。

 var result = from a in db.TableA join b in db.TableB on a.aId equals b.aId group new {A = a, B = b} by b.bValue; // demonstration of navigating the result foreach(var g in result) { Console.WriteLine(g.Key); foreach(var x in g) { Console.WriteLine(xAaId); Console.WriteLine(xBbId); } } 

您的result对象将是IQueryable> ,因此您必须访问其中一个结果集合,即IGrouping ,然后深入集合以获取x对象。