简体   繁体   English

LINQ Lambda,分组列表

[英]LINQ Lambda, Group by with list

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 Expression)到.GroupBy数据,而不是使用通常的.Sum()或.Count(),我希望结果数据是Int的列表。

I defined my own class named: Filter_IDs. 我定义了自己的类:Filter_IDs。 Its constructor needs two parameters: 它的构造函数需要两个参数:

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. 使用此查询不会产生构建错误,但会在RunTime上显示“NotSupportedException”。

The database looks like this to give you a better understanding of the data: 数据库看起来像这样可以让您更好地理解数据:

http://d.pr/i/mnhq+ (droplr image) http://d.pr/i/mnhq+(droplr image)

Thanks in advance, Gerben 提前谢谢,Gerben

I think the problem is the DB is not able to call ToList in the select, nor to create a new Filter_ID. 我认为问题是DB无法在select中调用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();

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 objectType and 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>();   
        }
    }    
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM