c# - 从 Entity 中为不同的内部实体的属性值选择替代元素并重复它们
问题描述
我有一个Product
包含实体的Category
实体。现在我想要实现的是一个产品列表,其中列表包含产品,其中一个类别的产品应该在列出所有其他类别的产品后出现。例如:如果有三个类别(cat1、cat2 和 cat3)并且每个类别包含 2 到 3 个产品并且pageSize
是 5,那么列表应该是这样的:
- [来自 cat1 的产品 1],
- [来自 cat2 的产品 2],
- [来自 cat3 的产品 3],
- [来自 cat1 的 Product4],
- [来自 cat2 的 Product5]
以下是我的代码。但它现在显示了正确的结果。
var randomProducts = new List<Product>();
var categories = Products.Select(c => c.Category.ID).Distinct().ToList();
int y = categories.First();
int index = 0;
for (int i = 0; i < pageSize; i++)
{
if (index == categories.Count)
break;
var catid = categories[index];
var _products = Products.Skip(skipCount).ToList().Except(randomProducts).Where(c => c.Category.ID == catid);
if(_products != null && _products.Count() > 0)
randomProducts.Add(_products.First());
else
{
index++;
}
if (randomProducts.Count == Products.Count())
break;
}
return randomProducts;
解决方案
推荐阅读
- google-cloud-platform - 通过使用适用于 Google Compute Engine API 的 Node.js 客户端库附加现有磁盘来创建新 VM
- configuration - 如何在独立模式下在集群中配置 jbossfuse 7.0
- bash - 带有 base64 的 Google Speech bash 脚本:意外的令牌。\n
- swift - 通过枚举显示多个 UICollectionViewCells?
- idl-programming-language - 使用 idl 的曲线下面积
- r - 将预测值添加到原始 df
- python - 如何更改 kivy Graph 的背景颜色?
- javascript - 从输入中检索数据 - laravel
- xslt - 按类型分组的 xslt 字段
- java - 如何使用 CSS 选择器在元素之间进行选择