Как сгруппировать IList <T>с помощью LINQ? - PullRequest
0 голосов
/ 07 декабря 2011
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();
}

Как мне сгруппировать по продукту

Ответы [ 3 ]

1 голос
/ 07 декабря 2011
group product by product.Attribute_1 into g

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

1 голос
/ 07 декабря 2011

Группировка может быть выполнена с помощью оператора groupq. Group *.

Синтаксис будет выглядеть примерно так:

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 }; 

Здесь вы можете найти образцы Linq дляgrouping.

Но я полагаю, вы хотите, чтобы все свойства unqiue Attribute_1 были возвращены из вашей функции?

Затем вы можете использовать оператор Distinct .Это подойдет для вашего списка строк Attribute_1, который будет содержать только уникальные значения.

1 голос
/ 07 декабря 2011
from product in context.Products
where product.ProductCategory_Id == productcatid
group product by product.Attribute_1 into g
select g;
...