有没有办法在 LINQ 中设置值?

发布于 2024-12-11 13:24:26 字数 205 浏览 0 评论 0原文

有没有更好的方法来使用 LINQ 完成这些作业?

IEnumerable<SideBarUnit> sulist1 = newlist.Where(c => c.StatusID == EmergencyCV.ID);
foreach (SideBarUnit su in sulist1) su.color = "Red";

Is there a better way to do these assignments with LINQ?

IEnumerable<SideBarUnit> sulist1 = newlist.Where(c => c.StatusID == EmergencyCV.ID);
foreach (SideBarUnit su in sulist1) su.color = "Red";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

誰ツ都不明白 2024-12-18 13:24:26

不。

您可以这样做,正如这里的其他答案所指出的那样,但是您将不再使用 LINQ

Eric Lippert 撰写了一篇关于这个主题,我强烈建议您阅读。以下是摘录:

出于两个原因,我在哲学上反对提供这样的方法。

第一个原因是这样做违反了所有其他序列运算符所基于的函数式编程原则。显然调用此方法的唯一目的是产生副作用。

表达式的目的是计算值,而不是产生副作用。

...

第二个原因是这样做会给语言增加零新的表示能力。这样做可以让您重写这段完全清晰的代码:

foreach(Foo foo in foos){涉及foo的语句; }

进入这段代码:

foos.ForEach((Foo foo)=>{涉及foo的语句; });

它使用几乎完全相同的字符,但顺序略有不同。然而第二个版本更难理解,更难调试,并且引入了闭包语义,从而可能以微妙的方式改变对象的生命周期。

No.

You can do such a thing, as the other answers here point out, but then you are no longer using LINQ.

Eric Lippert wrote an excellent blog post about this very subject, which I highly recommend you read. Here is an excerpt:

I am philosophically opposed to providing such a method, for two reasons.

The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects.

The purpose of an expression is to compute a value, not to cause a side effect.

...

The second reason is that doing so adds zero new representational power to the language. Doing this lets you rewrite this perfectly clear code:

foreach(Foo foo in foos){ statement involving foo; }

into this code:

foos.ForEach((Foo foo)=>{ statement involving foo; });

which uses almost exactly the same characters in slightly different order. And yet the second version is harder to understand, harder to debug, and introduces closure semantics, thereby potentially changing object lifetimes in subtle ways.

记忆消瘦 2024-12-18 13:24:26

Linq 的重点是选择,而不是更新。
但是您可以使用 ToList 和 List 的 ForEach 来更新元素:但

newlist.Where(c => c.StatusID == EmergencyCV.ID).
  ToList().
  ForEach(su => su.color = "Red");

我不确定可读性是否更好......

Linq is all about selecting, not updating.
But you could use ToList and List's ForEach to update the elements:

newlist.Where(c => c.StatusID == EmergencyCV.ID).
  ToList().
  ForEach(su => su.color = "Red");

I'm not sure the readability is better though...

会发光的星星闪亮亮i 2024-12-18 13:24:26

使用 List.ForEach() 方法:

IEnumerable<SideBarUnit> sulist1 = 
   newlist.Where(c => c.StatusID == EmergencyCV.ID)
          .ToList()
          .ForEach(item => item.color = "Red");

PS:我知道 Eric Lippert 的关于 ForEach() 与 foreach 的文章,但无论如何它很有用,我更喜欢它而不是 for/foreach 循环

编辑:关于评论中有关性能命中

Enumerable.ToList()实现的

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    return new List<TSource>(source);
}

问题,因此性能命中是创建新List<>()的时候实例。

MSDN 说接下来是 List<>(IEnumerable<>) ctor :

初始化包含元素的 List 类的新实例
从指定集合中复制,并且有足够的容量来容纳复制的元素数量。

所以会有一个所有列表的复制操作

Using List.ForEach() method:

IEnumerable<SideBarUnit> sulist1 = 
   newlist.Where(c => c.StatusID == EmergencyCV.ID)
          .ToList()
          .ForEach(item => item.color = "Red");

PS: I know about Eric Lippert's article aboout ForEach() vs foreach, but anyway it is useful and I preffer it rather than for/foreach loops

EDIT: regarding question in comment about performance hits

Enumerable.ToList() implemented as

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    return new List<TSource>(source);
}

so performance hit is a time to create a new List<>() instance.

MSDN says next for List<>(IEnumerable<>) ctor:

Initializes a new instance of the List class that contains elements
copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

So there is would be a copy operation of all list

合久必婚 2024-12-18 13:24:26

最简单的方法是定义一个 foreach 扩展方法,该方法允许您将其添加到此类查询的末尾。例如

public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) {
  foreach (var cur in enumerable) {
    action(cur);
  }
}

...

用法:

newlist
  .Where(c => c.StatusID == EmergencyCV.ID)
  .ForEach(cur => cur.color = "Red");

The simplest way is to define a foreach extension method which allows you to add it onto the end of a query of this sort. For example

public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) {
  foreach (var cur in enumerable) {
    action(cur);
  }
}

...

usage:

newlist
  .Where(c => c.StatusID == EmergencyCV.ID)
  .ForEach(cur => cur.color = "Red");
眸中客 2024-12-18 13:24:26

LINQ 的目的主要是查询/投影/转换数据。

您可能想要使用 ForEach 扩展方法。但请参阅此讨论了解更多详细信息。

LINQs purpose is mainly to query/project/transform data.

You might want to use the ForEach extension method. But see this discussion for more detail.

要走就滚别墨迹 2024-12-18 13:24:26

您可以在 Select 语句中使用匿名方法:

var sulist1 = newlist.Where(c => c.StatusID == EmergencyCV.ID)
                     .Select(c => {
                                     c.color = "Red";
                                     return c;
                                  });

You could use an anonymous method in a Select statement:

var sulist1 = newlist.Where(c => c.StatusID == EmergencyCV.ID)
                     .Select(c => {
                                     c.color = "Red";
                                     return c;
                                  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文