在linq中使用组时,Skip and take不起作用
尝试在linq中使用group by后的take和skip实现分页 假设分组后,我可以得到200个组,但我想在take和skip的基础上进行查询,只得到有限数量的组 这是我到目前为止所做的尝试在linq中使用组时,Skip and take不起作用,linq,Linq,尝试在linq中使用group by后的take和skip实现分页 假设分组后,我可以得到200个组,但我想在take和skip的基础上进行查询,只得到有限数量的组 这是我到目前为止所做的尝试 var data = db.TableStyle.GroupBy(p => p.Style) .Select(group => new
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()
};
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; }
}
过滤器模型过滤器
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()
};
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; }
}
公共类过滤器模型
{
公共整数跳过{get;set;}
公共整数Take{get;set;}
公共类别{get;set;}
公共列表品牌名称{get;set;}
公共列表颜色名称{get;set;}
公共列表SizeName{get;set;}
公共价格模式价格{get;set;}
公共字符串关键字{get;set;}
}
最后我使用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()
};
问题是什么?使用group时Skip and take不起作用,也对问题进行了编辑,但降级问题的意义何在,任何方式的帮助都将不胜感激:)因此您希望使用筛选器。Skip and filters。从每个组中选取,或跳过一些组,然后选取另一个组?只需在分页中跳过一些组,然后选取其他组,以及其他过滤器。