我有两张桌子
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};
我的代码无法识别组之后的连接.换句话说,分组工作,但连接不(或至少我无法弄清楚如何访问连接后的所有数据).
解决方法
group和the之间的表达式创建组元素.
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(x.A.aId); Console.WriteLine(x.B.bId); } }