繁体   English   中英

Sequence包含多个匹配元素

[英]Sequence contains more than one matching element

当我试图设置匹配条件的每个修整项目的IsDefault属性时,它会抛出错误说:

序列包含多个匹配序列。

(this.DressingItems
     .Where(xx => xx.DressingInfo.CatID == catId 
                        && xx.ProductID == this.ProductID)
     .Single()).IsDefault = false;

好吧,这个例外表明至少两个序列DressingItems匹配你的Where条件。 Single的调用会导致异常,因为它断言只传入一个项目。

阅读你的问题让我觉得你想对输入序列的每个项目做一些事情,所以你可能会使用foreach循环:

foreach(var item in this.DressingItems.Where(xx => xx.DressingInfo.CatID == catId && xx.ProductID == this.ProductID))
{
    item.IsDefault = false;
}
this.DressingItems.Where(x=> x.DressingInfo.CatID == catId && 
                                x.ProductID == this.ProductID).ToList()
                 .ForEach(item=>item.IsDefault = false);

Single运算符的要点是断言给定序列只有一个项。 例如,通过主键检索特定实例时。

我想你想要改变符合条件的任何DressingItem的状态,在这种情况下你有一些选项,都涉及枚举结果集,并执行一些行为。

没有LINQ运算符可以专门执行此操作,因为LINQ运算符应该是纯粹的。 纯函数是没有副作用的函数,这正是您要做的。

但是, List<T>上有一个扩展方法,允许这样做。 例如

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<T> ForEach<T>(
         this IEnumerable<T> source,
         Action<T> mutator)
    {
        var buffered = source.ToList();
        buffered.ForEach(mutator);
        return buffered;
    }
}

你可能会问为什么微软的人决定不把这个添加到BCL:我记得,这个想法是扩展方法与foreach() { }结构在打字方面不会带来太多好处,而且它不会'在模棱两可的方面提供帮助。 所有其他运算符都是无副作用的,并且这个运算符明确地设计用于诱导它们。

它是Single方法抛出的InvalidOperationException

该方法应该只返回一个元素,请检查您在查询中使用的条件。

但是,如果找不到任何元素,也会抛出异常

在this.DressingItems中有多个项目与给定的CatId和Product Id匹配。

如果您确定必须有一个(单个),那么您必须检查this.DressingItems是如何加载的。

如果预期有多个,那么你必须使用foreach来设置值。

由于您正在寻找一个衬垫,您可以创建自己的方法。

public static void DoActionForEachElement<T>(IEnumerable<T> items, Func<T, bool> predicate, Action<T> 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM