简体   繁体   中英

Updating single Column of List without Foreach Loop

I have a List

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

where Myclass is

public class Myclass
{
    public string ExamName { get; set; }
    public Nullable<decimal> Marks { get; set; }
    public Nullable<decimal> OutOfMarks { get; set; }
    public string GradeMarks { get; set; }
    public Nullable<decimal> markEaxmTotalOverAll { get; set; }
    public Nullable<decimal> MarksExamTotalMarksTypeWise { get; set; }
    public Nullable<decimal> MarksExamOutOfMarksTotalOverAll { get; set; }
    public Nullable<decimal> MarksExamOutOfMarksTotalMarksTypeWise { get; set; }
}

li contains almost 10000 items
i want to set markEaxmTotalOverAll value to 50 in all 10000 items
but don't want to do it with loop , Is there any way to update it , I tried this code

li.Select(s => { s.MarksExamTotalOverAll = 50; return s; }); 

but its not working

As the Q of LINQ says, the basic usage of LINQ is to query , not to update .

The less verbose form of foreach is the ForEach method of List<T> :

li.ForEach(z => z.MarksExamTotalOverAll = 50);

Try this code which runs specified task in a parallel manner

 List<Myclass> li = new List<Myclass>();
 Parallel.ForEach(li, l => l.MarksExamOutOfMarksTotalOverAll = 50);

使用List<T>.ForEach

li.ForEach(x=>x.MarksExamTotalOverAll = 50);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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