c# - 如何查找 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 }
};
解决方案
收集物品及其有效性:
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 关联的标志。
推荐阅读
- python - 如何防止两个相互更新的小部件的无限循环?
- javascript - 拥有一个包含大 DIV 并使用 JS 将其注入 html 的字符串是常见的做法吗
- amazon-web-services - 尝试将音频文件上传到 aws s3 存储桶时访问被拒绝
- ffmpeg - FFmpeg、librtmp和rtmpdump之间有什么关系?
- javascript - 单击页面中的href后向上滚动
- webpack - webpack 的脚本加载器替代方案?
- wear-os - 收集所有心率和其他数据的健身手表
- c++ - 加快高度模板化 C++ 软件开发的技巧是什么?
- javascript - 显示动态创建的可折叠内容
- flutter - 返回新屏幕后如何关闭搜索栏?扑