-1

I have a List of objects and I want to filter and update the value in a single linq? Is it possible? If not then what is the best way to do it?

PropertyNearBys = PropertyNearbys.Where(x => x.IsActive == true);
PropertyNearbys.ForEach(x => x.PropertyId = PropertyId);

Is there is a possibility of performing above two lines in single LINQ? I want recorded that are active and also want to set PropertyId in single statement

Haider Ali
  • 13
  • 3
  • It's unclear if you need filtered items after foreach or not? also if `PropertyNearbys` is `List` then you can't do `PropertyNearBys = PropertyNearbys.Where(..)` as `Where` returns `IEnumerable` – Selvin Mar 02 '22 at 10:11
  • Sure: `PropertyNearbys.Where(x => x.IsActive).ToList().ForEach(x => x.PropertyId = PropertyId)`. I wouldn't though, because it's a bit of an abuse of ToList just so you can use List's ForEach, when you can as easily foreach the Where – Caius Jard Mar 02 '22 at 10:25
  • I'm not too bothered tbh, Tim.. People inappropriately ToList all the time, all over the place, when they could simply enumerate an enumerable. We could chase the theoretical out to the nth degree on many things and the "you can, but you shouldn't" has been said by everyone here (including me) – Caius Jard Mar 02 '22 at 10:29
  • @Haider *performing above two lines in single LINQ* - I'd also like to emphasize that only the Where is LINQ. ForEach is not a LINQ thing, it's a List thing.. Nor should it be a LINQ thing because a major LINQ goal is that queries over collections shouldn't have side effects like modifying the collection. You really genuinely could use a Select to do the modification but you absolutely should not. There are no prizes at code review for the hero who squashes an entire algorithm into one torturoualy complex rule violating line of LINQ – Caius Jard Mar 02 '22 at 10:36
  • I agree with everything you're saying, Tim – Caius Jard Mar 02 '22 at 10:41
  • @Selvin I just noticed that there's a case difference on `b` in the two "nearbys" so technically one could be a List and the other an IE.. – Caius Jard Mar 02 '22 at 10:52
  • @Haider the only reasonable answer in the linked duplicate is the one posted by MakePeaceGreatAgain, btw – Caius Jard Mar 02 '22 at 11:19
  • PropertyNearBys = PropertyNearbys.Where(x => x.IsActive).Select(p=> {p.PropertyId = PropertyId; return p); – Abhay Prince Mar 06 '22 at 12:19

2 Answers2

1

Linq stands for Language-Integrated Query and it is used to query over the collection of data. It is not used to update any record/field/property from the collection.

.Where() clause returns IEnumerable<T>, not a list. .ForEach() is an extension method of List<T> class, so to answer your question,

I want to filter and update the value in a single linq?

Using combination of .Where() and .ForEach(), it is not possible.


If you ask what is simplest way to do this, then I would have solved it using simple foreach loop, like,

foreach(var PropertyNearby in PropertyNearbys)
{
    if(PropertyNearby.IsActive)
       PropertyNearby.PropertyId = PropertyId;
        
}
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

you should use a dto or an anonymous method like this:

PropertyNearBys = PropertyNearbys.Where(x => x.IsActive == true).Select(x=> new {IsActive, PropertyId =PropertyId});

Add as many properties inside select as you need.

Rajon Tanducar
  • 308
  • 4
  • 8