【问题标题】:How to group IList<T> using LINQ?如何使用 LINQ 对 IList<T> 进行分组?
【发布时间】:2011-12-07 14:38:07
【问题描述】:
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();
}

如何按 product.Attribute_1 分组?

【问题讨论】:

    标签: c# .net linq group-by ilist


    【解决方案1】:

    可以使用 group by linq 运算符进行分组。

    语法类似于:

    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你可以找到用于分组的Linq示例。

    但我想你想从你的函数返回所有 unqiue Attribute_1 属性?

    然后您可以使用Distinct 运算符。这将使您的 Attribute_1 字符串列表只包含唯一值。

    【讨论】:

      【解决方案2】:
      from product in context.Products
      where product.ProductCategory_Id == productcatid
      group product by product.Attribute_1 into g
      select g;
      

      【讨论】:

        【解决方案3】:
        group product by product.Attribute_1 into g
        

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

        【讨论】:

          猜你喜欢
          • 2010-11-01
          • 2012-01-08
          • 1970-01-01
          • 2013-06-09
          • 1970-01-01
          • 2018-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多