首页 > 解决方案 > 如何查找 ItemID 在列表或数组中是否有不同的 ItemCode?

问题描述

我在这里需要的是我想知道该 ItemId 是否具有不同的 ItemCode,因为在我的项目中,只有一个 ItemId 可以有一个 ItemCode,但位置可以尽可能不同。有什么办法可以解决这个问题吗?

这是我的代码:

Product[] products = { 
    new Product { ItemId= , 1001="apple", ItemCode=9, Location=store1 }, 
    new Product { ItemId= , 1001="apple", ItemCode=10, Location=store2 },
    new Product { ItemId= , 1002="apple", ItemCode=11, Location=store3 }, 
    new Product { ItemId= , 1002="apple", ItemCode=11, Location=store3 } 
};

标签: c#asp.netasp.net-mvc-5

解决方案


收集物品及其有效性:

var itemsValidity = 
    products.GroupBy(p => p.ItemId).
             Select(g => new
             {
                 ItemId = g.Key,
                 IsValid = !g.GroupBy(item => item.ItemCode).Skip(1).Any()
             });

将返回带有 ItemId 的对象集合和一个指示 ItemId 是否与多个 ItemCode 关联的标志。

小提琴


推荐阅读