C# 使用 LINQ 有条件地更新列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19930450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:09:05  来源:igfitidea点击:

conditional Updating a list using LINQ

c#linqlist

提问by Amit Bisht

I had a list

我有一个清单

List<Myclass> li = new List<Myclass>();

where Myclass is

Myclass在哪里

class Myclass
{
public string name {get;set;}
public decimal age {get;set;}
}

items in li looks like

li 中的项目看起来像

enter image description here

在此处输入图片说明

 i want to update `li` according to name but with `LINQ` like

li.where(w=> w.name = "di") = li.Where(w => w.name =="di").select(s => {s.age = 10;return s;}).Tolist();
li.where(w=> w.name = "marks") = li.Where(w => w.name =="marks").select(s => {s.age = 20;return s;}).Tolist();
li.where(w=> w.name = "grade") = li.Where(w => w.name =="grade").select(s => {s.age = 10;return s;}).Tolist();

and want result which looks like this

并想要看起来像这样的结果

enter image description here

在此处输入图片说明

my code gives error can you please tell how i do this

我的代码出错了,你能告诉我是怎么做的吗

采纳答案by Kamil Budziewski

cleanerway to do this is using foreach

更简洁的方法是使用 foreach

foreach(var item in li.Where(w => w.name =="di"))
{
   item.age=10;
}

回答by Baldrick

You need:

你需要:

li.Where(w=> w.name == "di").ToList().ForEach(i => i.age = 10);

Program code:

程序代码:

namespace Test
{
    class Program
    {
        class Myclass
        {
            public string name { get; set; }
            public decimal age { get; set; }
        }

        static void Main(string[] args)
        {
            var list = new List<Myclass> { new Myclass{name = "di", age = 0}, new Myclass{name = "marks", age = 0}, new Myclass{name = "grade", age = 0}};
            list.Where(w=> w.name == "di").ToList().ForEach(i => i.age = 10);
            list.ForEach(i => Console.WriteLine(i.name + ":" + i.age));
        }
    }
}

Output:

输出:

 di:10
 marks:0
 grade:0

回答by Smeegs

If you really want to use linq, you can do something like this

如果你真的想使用 linq,你可以这样做

li= (from tl in li
    select new Myclass
    {
        name = tl.name,
        age = (tl.name == "di" ? 10 : (tl.name == "marks" ? 20 : 30))

    }).ToList();

or

或者

li = li.Select(ex => new MyClass { name = ex.name, age = (ex.name == "di" ? 10 : (ex.name == "marks" ? 20 : 30)) }).ToList();

This assumes that there are only 3 types of name. I would externalize that part into a function to make it more manageable.

这假设只有 3 种类型的name. 我会将该部分具体化为一个函数,使其更易于管理。

回答by sivabalan

        li.Where(w => w.name == "di" )
          .Select(s => { s.age = 10; return s; })
          .ToList();

回答by FindOut_Quran

How about

怎么样

(from k in myList
 where k.id > 35
 select k).ToList().ForEach(k => k.Name = "Banana");

回答by Santosh

Try Parallelfor longer lists:

尝试Parallel获取更长的列表:

Parallel.ForEach(li.Where(f => f.name == "di"), l => l.age = 10);

回答by David Donari

Try this:

尝试这个:

li.ForEach(x => x.age = (x.name == "di") ?
                10 : (x.name == "marks") ?
                20 : (x.name == "grade") ?
                30 : 0 );

All values are updated in one line of code and you browse the List only ONE time. You have also a way to set a default value.

所有值都在一行代码中更新,您只需浏览列表一次。您还可以设置默认值。