C# LINQ Lambda,按列表分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13745904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ Lambda, Group by with list
提问by gerb0n
I'm having some trouble with finding the right syntax to accomplish the following:
我在找到正确的语法来完成以下任务时遇到了一些麻烦:
Is it possible with LINQ (Lambda Expression) to .GroupBy data and instead of using the usual .Sum() or .Count() I want the resulting data to be a List of Int.
是否可以使用 LINQ(Lambda 表达式)到 .GroupBy 数据,而不是使用通常的 .Sum() 或 .Count() 我希望结果数据是一个 Int 列表。
I defined my own class named: Filter_IDs. Its constructor needs two parameters:
我定义了自己的类,名为:Filter_IDs。它的构造函数需要两个参数:
public int? type; // Represents the object_type column from my database
public List<int?> objects; // Represents the object_id column from my database
I want to load data from my database into this object. The following LINQ query should result in a List of Filter_IDs:
我想将数据从我的数据库加载到这个对象中。以下 LINQ 查询应生成 Filter_ID 列表:
The following LINQ query should result in a List of Filter_IDs:
以下 LINQ 查询应生成 Filter_ID 列表:
List<Filter_IDs> filterids = ef.filterLine
.GroupBy(fl => fl.objectType)
.Select(fl => new Filter_IDs { type = fl.Key, objects = fl.Select(x => x.object_id).ToList() })
.ToList();
Using this query gives no building error but gives an 'NotSupportedException' on RunTime.
使用此查询不会产生构建错误,但会在运行时产生“NotSupportedException”。
The database looks like this to give you a better understanding of the data:
数据库看起来像这样,让您更好地理解数据:
http://d.pr/i/mnhq+(droplr image)
Thanks in advance, Gerben
提前致谢,格本
采纳答案by Kek
I think the problem is the DB is not able to call ToList in the select, nor to create a new Filter_ID.
我认为问题是数据库无法在选择中调用 ToList,也无法创建新的 Filter_ID。
Try something like this :
尝试这样的事情:
List<Filter_IDs> filterids = ef.filterLine.Select(o => new { objectType = o.objectType, object_id=o.object_id})
.GroupBy(fl => fl.objectType).ToList()
.Select(fl => new Filter_IDs { type = fl.Key, objects = fl.Select(x => x.object_id).ToList() })
.ToList();
回答by Kirill Bestemyanov
You can use GroupBy on client side:
您可以在客户端使用 GroupBy:
List<Filter_IDs> filterids = ef.filterLine
.Select(fl=>new {fl.ObjectType, fl.object_id})
.AsEnumerable()
.GroupBy(fl => fl.objectType)
.Select(fl => new Filter_IDs { type = fl.Key, objects = fl.Select(x => x.object_id).ToList() })
.ToList();
回答by Jodrell
Maybe you want
也许你想要
IList<Filter_IDs> filterIds = ef.filterline
.Select(fl => fl.objectType).Distinct()
.Select(ot => new Filter_IDs
{
type = ot,
objects = ef.filterline
.Where(fl => fl.objectType == ot)
.Select(fl =>objectType)
.ToList()
}).ToList();
Get the distinct list objectTypeand use that to subquery for each list of object_id.
获取不同的列表objectType并使用它来对object_id.
However, it seems more efficient to me to just enumerate the values in order,
但是,对我来说,按顺序枚举值似乎更有效,
var results = new List<Filter_IDs>();
var ids = new List<int>();
var first = true;
int thisType;
foreach (var fl in ef.filterLines
.OrderBy(fl => fl.objectType)
.ThenBy(fl => fl.object_Id))
{
if (first)
{
thisType = fl.objectType;
first = false;
}
else
{
if (fl.objectType == thisType)
{
ids.Add(fl.object_Id);
}
else
{
results.Add(new Filter_IDs
{
Type = thisType,
objects = ids
});
thisType = fl.objectType;
ids = new List<int>();
}
}
}

