PramodChoudhari PramodChoudhari - 22 days ago 9
C# Question

assign value using linq

public class Company
{
public int id { get; set; }
public int Name { get; set; }
}

List<Company> listofCompany = new List<Company>();


this is my collection of company list I want to assign values to Name property using LINQ

listofCompany.Where(d => d.Id = 1);


(I want to assing name property of company id 1)

how do I assign it.?

Answer

using Linq would be:

 listOfCompany.Where(c=> c.id == 1).FirstOrDefault().Name = "Whatever Name";

UPDATE

For multiple items (condition is met by multiple items):

 listOfCompany.Where(c=> c.id == 1).ToList().ForEach(cc => cc.Name = "Whatever Name");
Comments