Change the property of objects in a List using LINQ

Vahid

I have list of Beam objects. How can I change the IsJoist property of the beams when the Width property is greater than 40 using LINQ?

class Beam
{
    public double Width { get; set; }
    public bool IsJoist { get; set; }
}

var bm1 = new Beam { Width = 40 };
var bm2 = new Beam { Width = 50 };
var bm3 = new Beam { Width = 30 };
var bm4 = new Beam { Width = 60 };

var Beams = new List<Beam> { bm1, bm2, bm3, bm4 };

Here is what I have done but I'm only getting a List. I want the new list to be the same as the original one except that the IsJoist property for some beams will be set to true.

var result = Beams
    .Where(x => x.Width > 40)
    .Select(x => x.IsJoist = true)
    .ToList();

I was able to implement this as below. Is it ok since LINQ is meant for queries?

var result = Beams
    .Where(x => x.Width > 40)
    .Select(x =>
    {
        x.IsJoist = true;
        return x;
    })
    .ToList();
Eric J.

If your solution must be completely Linq, you could do

Beams.Where(x => x.Width > 40).ToList().ForEach(b => b.IsJoist = true);

However, that is not an ideal way to implement this (@Jacob's answer is a better one). Check out Eric Lippert's blog entry on the topic. The most important lines for me are

The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects. The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect.

https://ericlippert.com/2009/05/18/foreach-vs-foreach/

Note that ToList() is called because List<T> provides a ForEach() method, while Linq in general does not offer such an extension method for the reasons Eric Lippert cites in that blog entry.

UPDATE

Your code both updates entities in the original list (changes IsJoist to true for certain conditions) and returns references to the objects that were updated. If that is what you intended, the code functions. However, Linq is designed with a functional paradigm in mind. Introducing side effects within the context of a Linq expression violates the functional programming principal behind the extension methods.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change the property of objects in a List using LINQ

From Dev

Change a list's property using linq

From Dev

Group objects in List by property in object that is List using LINQ

From Dev

Change the type of one of the objects property and sort it using LINQ

From Dev

Get the objects with the maximum value for a specific property from a list using LINQ

From Dev

Using LINQ, get a list of objects that contain a particular property value

From Dev

Get the objects with the maximum value for a specific property from a list using LINQ

From Dev

Using LINQ, get a list of objects that contain a particular property value

From Dev

Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate

From Dev

Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate

From Dev

Paging List of Objects using LINQ

From Dev

Using LINQ to get the difference between two list of objects based only on a single object property

From Dev

How to determine if all objects inside List<T> has the same property value using Linq

From Dev

Linq - get objects with a list property that does not contain values in another list

From Dev

Using Linq to concatenate a list of property of classes

From Dev

Reset(not removing) property for duplicates in a list using linq

From Dev

update list property using linq join on dictionary

From Dev

Merge nested objects in one list using Linq

From Dev

using dynamic linq to filter a list of objects

From Dev

Linq: check if one of the property in a list is null for all the objects

From Dev

Add List objects from a single list to a new List using LINQ

From Dev

Change value in List<T> using Linq

From Dev

using LINQ to get a particular set of objects from a list of objects

From Dev

Using LINQ to get a distinct list of Parameters from a list of objects

From Dev

Access two indexes in a list and create a list of new objects using Linq

From Dev

Mapping a list of type string to a list of objects with a string property using automapper

From Dev

How to extract list of single property of objects in list using SpEL?

From Dev

Mapping a list of type string to a list of objects with a string property using automapper

From Dev

Using linq to iterate through winform menuitems and change a property value

Related Related

  1. 1

    Change the property of objects in a List using LINQ

  2. 2

    Change a list's property using linq

  3. 3

    Group objects in List by property in object that is List using LINQ

  4. 4

    Change the type of one of the objects property and sort it using LINQ

  5. 5

    Get the objects with the maximum value for a specific property from a list using LINQ

  6. 6

    Using LINQ, get a list of objects that contain a particular property value

  7. 7

    Get the objects with the maximum value for a specific property from a list using LINQ

  8. 8

    Using LINQ, get a list of objects that contain a particular property value

  9. 9

    Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate

  10. 10

    Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate

  11. 11

    Paging List of Objects using LINQ

  12. 12

    Using LINQ to get the difference between two list of objects based only on a single object property

  13. 13

    How to determine if all objects inside List<T> has the same property value using Linq

  14. 14

    Linq - get objects with a list property that does not contain values in another list

  15. 15

    Using Linq to concatenate a list of property of classes

  16. 16

    Reset(not removing) property for duplicates in a list using linq

  17. 17

    update list property using linq join on dictionary

  18. 18

    Merge nested objects in one list using Linq

  19. 19

    using dynamic linq to filter a list of objects

  20. 20

    Linq: check if one of the property in a list is null for all the objects

  21. 21

    Add List objects from a single list to a new List using LINQ

  22. 22

    Change value in List<T> using Linq

  23. 23

    using LINQ to get a particular set of objects from a list of objects

  24. 24

    Using LINQ to get a distinct list of Parameters from a list of objects

  25. 25

    Access two indexes in a list and create a list of new objects using Linq

  26. 26

    Mapping a list of type string to a list of objects with a string property using automapper

  27. 27

    How to extract list of single property of objects in list using SpEL?

  28. 28

    Mapping a list of type string to a list of objects with a string property using automapper

  29. 29

    Using linq to iterate through winform menuitems and change a property value

HotTag

Archive