如何加入Linq Query Group By的组项目? [英] How to join group item of Linq Query Group By?
问题描述
Letter(Id,Name)
SendLetter (Id,LetterId)
ReciveLetter(Id,SendLetterId,Type,User)
我想得到ReciveLetter的记录,按Group SenLetterId
& 类型
。
我使用这个查询。
<$ p (d.SendLetter.LetterId == LetterId)
group d by new {d.SendLetter,d.SendType} $ d $ db $进入g
选择新项目{......};
它是由SendLetterId和Type组成的项目。我想为每个分组的项目选择UserName的连接。
例如:
SendLetter:
1 1
2 1
ReciveLetter:
1 1 Type1 A
1 1 Type1 B
1 1 Type2 C
1 2 Type1 D
1 2 Type1 E
结果:
1 1 Type1 A,B
1 1 Type2 C
2 1 Type1 D,E
c $ c> Item 看起来像并且不知道你使用的是什么类型的LINQ,我可以给你一个应该用于linq-to-sql和实体框架的一般方法。
var query = from db.ReceiveLetter
where(d.SendLetter.LetterId == LetterId)
group d by new { d.SendLetter,d.SendType}转换为g
选择新的{g.Key.Sendletter,
g.Key.SendType,
Users = g.Select(x => x.Name)
};
var dashboard = query.AsEnumerable()
.Select(x => new Item {x.SendLetter.Id,
x.SendLetter.LetterId,
x.SendType,
Names = string.Join(,,x.Users)
});
AsEnumerable()
将执行切换到linq为了执行 string.Join
。
I have three tables that are related.
Letter(Id,Name)
SendLetter(Id,LetterId)
ReciveLetter(Id,SendLetterId,Type,User)
I want to get records of ReciveLetter that Group By SenLetterId
& Type
.
I use this query.
var dashbord = from d in db.ReceiveLetter
where (d.SendLetter.LetterId == LetterId)
group d by new { d.SendLetter, d.SendType } into g
select new Items{ ...... };
It is group Items by SendLetterId and Type. I want to select join of UserName for each grouped item.
for example:
SendLetter:
1 1
2 1
ReciveLetter:
1 1 Type1 A
1 1 Type1 B
1 1 Type2 C
1 2 Type1 D
1 2 Type1 E
Result:
1 1 Type1 A,B
1 1 Type2 C
2 1 Type1 D,E
Without knowing what Item
looks like and not knowing what type of LINQ you use I can give you a general approach that should work with linq-to-sql and entity framework.
var query = from d in db.ReceiveLetter
where (d.SendLetter.LetterId == LetterId)
group d by new { d.SendLetter, d.SendType } into g
select new { g.Key.Sendletter,
g.Key.SendType,
Users = g.Select(x => x.Name)
};
var dashboard = query.AsEnumerable()
.Select(x => new Item { x.SendLetter.Id,
x.SendLetter.LetterId,
x.SendType,
Names = string.Join(", ", x.Users)
});
AsEnumerable()
switches the execution to linq-to-objects in order to execute the string.Join
.
这篇关于如何加入Linq Query Group By的组项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!