在linq中使用group时跳过并且不能正常工作

时间:2017-06-28 13:03:43

标签: linq

尝试在linq

中使用take和skip in group after实现分页

假设在分组后我能够获得200个组,但我想在获取和跳过的基础上查询以获得有限数量的组。

这是我到目前为止所尝试的内容

var data = db.TableStyle.GroupBy(p => p.Style)                    
                     .Select(group =>
                        new
                        {
                            Style = group.Key,
                            Group = group.OrderBy(x => x)
                        }).Skip(filters.Skip).Take(filters.Take).ToList();

之前我使用下面的代码实现了它,但对于一组5000个组而言太慢了

 var data = (from p in db.TableStyles.AsQueryable()
                            group p by p.Style into gs
                            select new
                            {
                                Style = gs.Key,
                                ColorAvailable = gs.Select(c => new ColorModel { ColorImage = c.ColorImage, ColorName = c.ColorColorName }).Distinct().ToList(),                               
                                Keyword = gs.Select(b => b.Keywords).FirstOrDefault(),
                                UniqueId = gs.Select(b => b.UniqueId).ToList()
                            });


                if (filters.ColorName != null)
                {
                    data = data.Where(p => filters.ColorName.Any(t => p.ColorAvailable.Select(c => c.ColorName).Contains(t)));
                }



                return new StyleModal()
                {
                    Product = data.OrderBy(p => p.Style).ThenBy(p => p.ProductStatus).Take(filters.Take).Skip(filters.Skip).Select(p => new ProductDetail
                    {
                        BrandName = p.BrandName,
                        CategoryId = p.CategoryId,
                        ColorAvailable = p.ColorAvailable,
                        ProductDescription = p.ProductDescription,
                        ColorProductImage = p.ColorProductImage,
                        ProductStatus = p.ProductStatus,
                        ProductTitle = p.ProductTitle,
                        SizeAvailable = p.SizeAvailable,
                        SizeAvailableRange = p.SizeAvailableRange,
                        Style = p.Style,
                        TitleImage = p.TitleImage,
                        MaxPiecePrice = p.MaxPiecePrice,
                        MinPiecePrice = p.MinPiecePrice

                    }).ToList(),
                    ProductCount = data.Select(c => c.Style).Count()
                };

FilterModel过滤器

 public class FilterModel
    {
        public int Skip { get; set; }

        public int Take { get; set; }

        public Category Category { get; set; }

        public List<string> BrandName { get; set; }

        public List<string> ColorName { get; set; }

        public List<string> SizeName { get; set; }

        public PriceModal Price { get; set; }

        public string Keyword { get; set; }

    }

1 个答案:

答案 0 :(得分:0)

最后,我使用let关键字

实现了快速查询
var data = (from p in db.TableStyles.AsQueryable()
                           group p by p.Style into gs
                           let singleRow = gs.FirstOrDefault()
                           let groupData  = gs.Select(c => new
                           {
                               ColorAvailable = new ColorModel
                               {
                                   ColorImage = c.ColorImage,
                                   ColorName = c.ColorColorName

                               }
                           })
                           select new
                {
                    Style = gs.Key,
                    ColorAvailable = groupData.Select(c => c.ColorAvailable).Distinct().ToList(),
                    Keyword = singleRow.Keywords
                });


                if (filters.ColorName != null)
                {
                    data = data.Where(p => filters.ColorName.Any(t => p.ColorAvailable.Select(c => c.ColorName).Contains(t)));
                }



                return new StyleModal()
                {
                    Product = data.OrderBy(p => p.Style).ThenBy(p => p.ProductStatus).Take(filters.Take).Skip(filters.Skip).Select(p => new ProductDetail
                    {
                        BrandName = p.BrandName,
                        CategoryId = p.CategoryId,
                        ColorAvailable = p.ColorAvailable,
                        ProductDescription = p.ProductDescription,
                        ColorProductImage = p.ColorProductImage,
                        ProductStatus = p.ProductStatus,
                        ProductTitle = p.ProductTitle,
                        SizeAvailable = p.SizeAvailable,
                        SizeAvailableRange = p.SizeAvailableRange,
                        Style = p.Style,
                        TitleImage = p.TitleImage,
                        MaxPiecePrice = p.MaxPiecePrice,
                        MinPiecePrice = p.MinPiecePrice

                    }).ToList(),
                    ProductCount = data.Select(c => c.Style).Count()
                };