PramodChoudhari PramodChoudhari - 1 year ago 110
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 Source

using Linq would be:

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


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

 listOfCompany.Where(c=> == 1).ToList().ForEach(cc => cc.Name = "Whatever Name");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download