如何通过LINQ执行组​​并获得IQueryable的或自定义类的对象? [英] How to perform group by in LINQ and get a Iqueryable or a Custom Class Object?

查看:162
本文介绍了如何通过LINQ执行组​​并获得IQueryable的或自定义类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的查询 -

Here is my query -

var data = Goaldata.GroupBy(c => c.GoalId).ToList();

这会返回一个对象Igrouping,我想一个IQueryable对象,我可以直接查询通过使用foreach()来获取数据,而在这种情况下,我不得不循环,然后获取数据。是否有LINQ另一种方式来组由直接返回一样类似于为了在LINQ会发生什么IQueryable的列表或一个列表。

This returns a Igrouping object and I want an Iqueryable object which I can directly query to get the data while in this case I have to loop through using a foreach() and then get the data. Is there another way to group by in LINQ which returns directly as a list of Iqueryable or a List as similar to what happens for order by in LINQ.

推荐答案

最简单的方法可能是

var data = Goaldata.GroupBy(c => c.GoalId).SelectMany(c => c).ToList();

在面向对象的意义上,它们并没有真正分组,但与基团一起,他们是有序的。

In the OO sense they aren't really grouped, but they are ordered with the groups together.

这篇关于如何通过LINQ执行组​​并获得IQueryable的或自定义类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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