Lambda 表达式 - 根据对象集合中另一个属性的值设置对象集合中另一个属性的值

发布于 2024-09-03 06:22:16 字数 656 浏览 2 评论 0原文

我是 lambda 表达式的新手,希望利用语法根据集合中的另一个值设置集合中一个属性的值

通常我会执行一个循环:

class Item
{
    public string Name { get; set; }
    public string Value { get; set; }
}

void Run()
{
    Item item1 = new Item { Name = "name1" };
    Item item2 = new Item { Name = "name2" };
    Item item3 = new Item { Name = "name3" };

    Collection<Item> items = new Collection<Item>() { item1, item2, item3 };

    // This is what I want to simplify.
    for (int i = 0; i < items.Count; i++)
    {
        if (items[i].Name == "name2")
        {
            // Set the value.
            items[i].Value = "value2";
        }
    }
}

I'm new to lambda expressions and looking to leverage the syntax to set the value of one property in a collection based on another value in a collection

Typically I would do a loop:

class Item
{
    public string Name { get; set; }
    public string Value { get; set; }
}

void Run()
{
    Item item1 = new Item { Name = "name1" };
    Item item2 = new Item { Name = "name2" };
    Item item3 = new Item { Name = "name3" };

    Collection<Item> items = new Collection<Item>() { item1, item2, item3 };

    // This is what I want to simplify.
    for (int i = 0; i < items.Count; i++)
    {
        if (items[i].Name == "name2")
        {
            // Set the value.
            items[i].Value = "value2";
        }
    }
}

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

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

发布评论

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

评论(1

不喜欢何必死缠烂打 2024-09-10 06:22:17

LINQ 通常对于选择数据比修改数据更有用。但是,您可以编写如下内容:

foreach(var item in items.Where(it => it.Name == "name2")) 
  item.Value = "value2";

首先选择需要修改的项目,然后使用标准命令式循环修改所有项目。您可以将 foreach 循环替换为可用于列表的 ForAll 方法,但我认为这不会给您带来任何优势:

items.Where(it => it.Name == "name2").ToList()
     .ForEach(it => it.Value = "value2");

请注意,您需要添加 ToList 在中间,因为 ForEach 是一项 .NET 2.0 功能,仅适用于 List 类型 - 不适用于所有 IEnumerable类型。 类型(与其他 LINQ 方法一样)。如果您喜欢这种方法,您可以为 IEnuerable 实现 ForEach

public static void ForEach<T>(this IEnumerable<T> en, Action<T> f) {
  foreach(var a in en) f(a);
}

// Then you can omit the `ToList` conversion in the middle 
items.Where(it => it.Name == "name2")
     .ForEach(it => it.Value = "value2");

无论如何,我更喜欢 foreach 循环,因为这也使得很明显,您正在做一些修改 - 在代码中轻松看到这一事实很有用。

LINQ is generally more useful for selecting data than for modifying data. However, you could write something like this:

foreach(var item in items.Where(it => it.Name == "name2")) 
  item.Value = "value2";

This first selects items that need to be modified and then modifies all of them using a standard imperative loop. You can replace the foreach loop with ForAll method that's available for lists, but I don't think this gives you any advantage:

items.Where(it => it.Name == "name2").ToList()
     .ForEach(it => it.Value = "value2");

Note that you need to add ToList in the middle, because ForEach is a .NET 2.0 feature that's available only for List<T> type - not for all IEnumerable<T> types (as other LINQ methods). If you like this approach, you can implement ForEach for IEnuerable<T>:

public static void ForEach<T>(this IEnumerable<T> en, Action<T> f) {
  foreach(var a in en) f(a);
}

// Then you can omit the `ToList` conversion in the middle 
items.Where(it => it.Name == "name2")
     .ForEach(it => it.Value = "value2");

Anyway, I'd prefer foreach loop, because that also makes it clear that you're doing some mutation - and it is useful to see this fact easily in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文