Sequence包含多个匹配元素
当我尝试设置匹配条件的每个修整项目的IsDefault
属性时,它会抛出错误说:
序列包含多个匹配序列。
(this.DressingItems .Where(xx => xx.DressingInfo.CatID == catId && xx.ProductID == this.ProductID) .Single()).IsDefault = false;
this.DressingItems.Where(x=> x.DressingInfo.CatID == catId && x.ProductID == this.ProductID).ToList() .ForEach(item=>item.IsDefault = false);
好吧,这个例外说, DressingItems
项目序列中至少有两项与你的Where
条件匹配。 对Single
的调用然后导致exception,因为它断言只传入一个项目。
阅读你的问题让我觉得你想对输入序列的每个项目做一些事情,所以你可能会使用foreach循环:
foreach(var item in this.DressingItems.Where(xx => xx.DressingInfo.CatID == catId && xx.ProductID == this.ProductID)) { item.IsDefault = false; }
Single
运算符的要点是断言给定序列只有一个项。 例如,当通过主键检索特定实例时。
我想你想改变符合条件的任何DressingItem
的状态,在这种情况下你有一些选项,都涉及枚举结果集,并执行一些行为。
没有专门执行此操作的LINQ运算符,因为LINQ运算符应该是纯粹的。 纯函数是没有副作用的函数,这正是您要做的。
但是, List
上有一个扩展方法,允许这样做。 例如
this.DressingItems.Where(di => di.DressingInfo.CatID == catId && di.ProductID == this.ProductID) .ToList() .ForEach(di => { di.IsDefault = false });
或者你可以自己动手:
public static class EnumerableExtensions { public static IEnumerable ForEach ( this IEnumerable source, Action mutator) { var buffered = source.ToList(); buffered.ForEach(mutator); return buffered; } }
你可能会问为什么微软的人决定不把这个添加到BCL:我记得,这个想法是扩展方法与foreach() { }
结构在打字方面不会带来太大的好处,而且它不会’在模糊性方面提供帮助。 所有其他操作符都是无副作用的,并且这个操作符明确地设计用于诱导它们。
它是Single
方法抛出的InvalidOperationException
。
该方法应该只返回一个元素,请检查您在查询中使用的条件。
但是,如果找不到任何元素,也会抛出exception
您在this.DressingItems中有多个与给定CatId和Product Id匹配的项目。
如果您确定必须有一个(单个),那么您必须检查this.DressingItems是如何加载的。
如果预期有多个,那么你必须使用foreach来设置值。
由于您正在寻找一个衬垫,您可以创建自己的方法。
public static void DoActionForEachElement(IEnumerable items, Func predicate, Action action) { foreach (var item in items) { if (predicate(item)) action(item); } }
然后通过它来调用它
DoActionForEachElement( DressingItems, xx => xx.DressingInfo.CatID == catId && xx.ProductID == ProductID, x => x.IsDefault = false);
这样,您就不必先将结果从Where
转换为“ List
。