简体   繁体   English

如何对IList进行分组 <T> 使用LINQ?

[英]How to group IList<T> using LINQ?

public static IList<string> GetAttribute_1(ModelContainer context, long productcatid)
{
    var query = from product in context.Products
                where product.ProductCategory_Id == productcatid
                select product.Attribute_1;

    return query.ToList();
}

how do i group by product.Attribute_1? 如何按product.Attribute_1分组?

from product in context.Products
where product.ProductCategory_Id == productcatid
group product by product.Attribute_1 into g
select g;
group product by product.Attribute_1 into g

http://www.hookedonlinq.com/GroupByOperator.ashx

Grouping can be done by using the group by linq operator. 可以使用linq运算符进行分组。

The syntax would be something like: 语法类似于:

var query = from product in context.Products
                        where product.ProductCategory_Id == productcatid
                        group product by prodyct.Attribute_1 into g
                        select new { Attribute_1 = g.Key, Products = g }; 

Here you can find Linq samples for grouping. 在这里,您可以找到用于分组的Linq样本。

But I suppose you want all the unqiue Attribute_1 properties returned from your function? 但是我想您希望从函数中返回所有unqiue Attribute_1属性?

You could then use the Distinct operator. 然后,您可以使用Distinct运算符。 That will fiter your list of Attribute_1 strings to contain only unique values. 这将适合您的Attribute_1字符串列表,使其仅包含唯一值。

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

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