联接两个表并使用linq将它们分组后,访问所有数据 [英] Access all of the data after joining two tables and group them using linq

查看:124
本文介绍了联接两个表并使用linq将它们分组后,访问所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个桌子

TableA
aId
aValue

TableB
bId
aId
bValue

我想通过aId联接这两个表,然后从那里按bValue

I want to join these two tables via aId, and from there, group them by 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};

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

My code doesn't recognize the join after the group. In other words, the grouping works, but the join doesn't (or at least I can't figure out how to access all of the data after the join).

推荐答案

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

The expression between the group and the by creates the group elements.

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);
  }
}

这篇关于联接两个表并使用linq将它们分组后,访问所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆