0

Please, refer to this other post: assign value using linq

With this expression we can update one value in multiple items of a list:

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

Is it possible to update more than one value? Is this more efficient that implementing a foreach structure and set the multiple values in it?

1
  • 2
    Is this more efficient that implementing a foreach structure I would say no. Creating a List first (which already enumerates the collection) only to use its ForEach method, is not more efficient than doing a foreach directly on the enumeration Commented May 12, 2017 at 6:55

5 Answers 5

3

Just use brackets:

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

Or use a ref to a method:

listOfCompany.Where(c=> c.id == 1).ToList().ForEach(UpdateCompany);

private void Function(Company cc) 
{    
    cc.Name = "Whatever Name";
    cc.Name = "Whatever Name";
    cc.Name = "Whatever Name";
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it like this:

listOfCompany.Where(c=> c.id == 1).ToList().ForEach(cc =>
{ 
    cc.Name = "Whatever Name";
    cc.OtherField="Whatever field";
});

Comments

2

LINQ is fun and all, but IMHO you should try to go for readable and maintainable code. When you need to update several fields I would argue that a regular foreach would suit you better.

foreach(var item in listOfCompany.Where(c=> c.id == 1))
{
    item.Name = "Whatever Name";
    item.Property = 1;
    //..
}

As for if it's more efficient, I don't think you would notice it. You can always study the reference source - it's basically only calling an Action for each item.

1 Comment

I second the maintainable hint. Regretted every single time when I thought I should do some smartass linq inline assignment. That being said, I found some of my legacy code, where I used Zip to merge-assign a collection of values into a collection of items. In retrospect, code older than a year looks like shit
0

try code Just use { } and Every item End use ; semicolon

listOfCompany.Where(c=> c.id == 1).ToList().ForEach(cc => { cc.Name = "Whatever Name";cc.Contacts="Manager";});

Comments

0

Approach without ForEach and only one iteration instead of two

listOfCompany = listOfCompany.Select(x => { 
                              x.Name = x.id == 1 ? "Whatever Name" : x.Name; 
                              x.Name2 = x.id == 1 ? "Whatever Name" : x.Name2;   
                              return x; 
                              }).ToList();

Approach without Linq (probably the fastes one)

for (int i = 0; i < listOfCompany.Count; i++)
{
    if (listOfCompany[i].id == 1)
    {
        listOfCompany[i].Name = "Whatever Name";
        listOfCompany[i].Name2 = "Whatever Name";
    }
}

3 Comments

It's probably worth noting that this goes against the spirit of LINQ...
@JonSkeet imo it's prettier and faster compared to .Where().ToList().ForEach()
It's very easy to make mistakes with it though - e.g. someone thinking "Hang on, I don't need a new list... I'll just take out the assignment and the ToList call" at which point it doesn't do anything due to lazy evaluation. Simply using a foreach loop is the clearest approach IMO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.