【发布时间】:2014-01-29 13:04:24
【问题描述】:
我正在检查两个列表中的元素并尝试添加到另一个列表:
List<ProductPacking> prodSubstitute = new List<ProductPacking>();
tempList = new Dictionary<string, string>();
List<string> prodIDList = new List<string>();
for (int count = 0; count < prodSubstitute.Count; count++)
{
foreach (string key in tempList.Keys)
{
if (!prodSubstitute.Any(i => i.id == key))
{
prodIDList.Add(prodSubstitute[count].id);
}
}
}
假设我对 prodSubstitute 的测试 ID 是 1,5,4,2,3。 tempList id 中的元素是 1,2,3。当我通过 prodSubstitute 循环时,如果 prodSubstitute 不包含 tempList 中的 id,它应该添加到 prodIDList 中。但是,使用此 LINQ 查询,它只会一直返回 null 而不是 5,4。
有什么线索吗?提前致谢。
编辑
if (!prodIDList.Contains(prodSubstitute[count].id) && !(lstCategory.Where(x => x.Equals(categoryName)).Select(x => x).Count() >= 2))
{
prodIDList.Add(prodSubstitute[count].id);
lstCategory.Add(categoryName);
}
【问题讨论】:
-
如果将 prodSubstitute.Any 更改为 prodSubstitute.Contains 会怎样?
-
你的意思是 prodSubstitute.Contains(key) ?它向我显示了有关最佳重载方法的错误消息。包含无效参数
-
我很抱歉,这是我的想法,这是错误的。 (虽然您可能可以使用比较器这样做,但这不是我的想法)
-
没问题但还是非常感谢:)
-
有什么意见吗?还没有解决