LINQ is better for querying, it is not a good option for modification.
You can modify the list items like:
foreach (var item in sorts)
{
if (item.DisplayName.Equals("abc", StringComparison.InvariantCultureIgnoreCase))
{
item.DisplayName = "Testing";
}
}
OR
foreach (var item in
sorts.Where(r=> r.DisplayName.Equals("abc", StringComparison.InvariantCultureIgnoreCase)))
{
item.DisplayName = "Testing";
}
Use string.Equals overload for case insenstive comparison.
There is one pure LINQ way of doing it but please don't. LINQ is for querying, not for modification. It should only work if SortElement
is a class, not a struct, because of reference type passed to lambda.
sorts.Select(r => r.DisplayName.Equals("abc", StringComparison.InvariantCultureIgnoreCase)
? r.DisplayName = "Testing"
: r.DisplayName ).ToList();
The above would work, even without assigning the result back to sorts
. But it should be avoided.