c#Linq - 从每个组中选择前2名

时间:2015-10-15 14:22:27

标签: c# linq group-by

我需要为每个ProductTypeName获取前2个产品:

var productDetails = (from p in products
                            join po in productOrganisations 
                            on p.Id equals po.ProductId
                            where po.OrganisationId == id
                            where p.ProductTypeId == (typeId > 0 ? typeId : p.ProductTypeId) //filter by type if specified
                            where p.IsLive
                            select new
                            {
                                Id = p.Id,
                                Name = p.Name,
                                SupplierName = p.Supplier.Name,
                                ProductTypeName = p.ProductType.Name,
                                ShortDescription = p.ShortDescription,
                                ProductTypeId = p.ProductTypeId,
                                DatePublished = p.DatePublished,
                                CurrencyId = p.CurrencyId,

                            })
                            .AsNoTracking()
                            .ToArray();

目前上述声明会返回我的所有产品,数百个。每个产品都有一个属性ProductTypeName。

我需要通过此ProductTypeName进行分组,然后通过datepublished降序获得前两个IN EACH GROUP。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

对于有同样问题的人,我最终使用以下方法解决:

.GroupBy(x => x.ProductTypeId)
.SelectMany(x => x.Take(2))

所以完整的例子是:

var productDetails = (from p in products
                            join po in productOrganisations 
                            on p.Id equals po.ProductId
                            where po.OrganisationId == id
                            where p.ProductTypeId == (typeId > 0 ? typeId : p.ProductTypeId) //filter by type if specified
                            where p.IsLive
                            select new
                            {
                                Id = p.Id,
                                Name = p.Name,
                                SupplierName = p.Supplier.Name,
                                ProductTypeName = p.ProductType.Name,
                                ShortDescription = p.ShortDescription,
                                ProductTypeId = p.ProductTypeId,
                                DatePublished = p.DatePublished,
                                CurrencyId = p.CurrencyId,
                            })
                            .GroupBy(x => x.ProductTypeId)
                            .SelectMany(x => x.Take(2))
                            .AsNoTracking()
                            .ToArray();

这有效,但我想知道这是最好的方法吗?效率最高?

答案 1 :(得分:2)

ChrisWue provides a good answer here。如果这解决了你的问题,请向他投票

强力输入您的列表,然后使用GroupByOrderByTake来获取结果。

强烈输入您的结果集:

List<ProductDetails> myProductList = (from p in products
                        join po in productOrganisations 
                        on p.Id equals po.ProductId
                        where po.OrganisationId == id
                        where p.ProductTypeId == (typeId > 0 ? typeId : p.ProductTypeId) //filter by type if specified
                        where p.IsLive
                        select new
                        {
                            Id = p.Id,
                            Name = p.Name,
                            SupplierName = p.Supplier.Name,
                            ProductTypeName = p.ProductType.Name,
                            ShortDescription = p.ShortDescription,
                            ProductTypeId = p.ProductTypeId,
                            DatePublished = p.DatePublished,
                            CurrencyId = p.CurrencyId,

                        })
                        .AsNoTracking()
                        .ToList();

然后像这样查询列表:

List<ProductDetail> finalResult = 
                     myProductList.GroupBy(p => p.ProductTypeName)
                     .SelectMany(d => d.OrderBy(r => r.DatePublished.Take(2))
                     .ToList();