C#LINQ:加入和组 [英] C# LINQ: Join and Group

查看:139
本文介绍了C#LINQ:加入和组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表

TableA
aId
aValue

TableB
bId
aId
bValue

我想加入这些通过援助两个表,并从那里通过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).

任何帮助,将不胜感激。我是一个的n00b。

Any help would be appreciated. I'm a n00b.

推荐答案

组之间的表达和在创建组元素。

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

这篇关于C#LINQ:加入和组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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