Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Syntax Any()

Tags:

c#

linq

any

I have this pseudo object

list<ListOfCars>{
   list<ListOfParts>
}

I currently have this query

ListOfCars.Where(x => x.ListofParts.Any(y => y.PartIsDeleted == false));

On my investigation, it returns something if atleast 1 item satisfies the condition in the Any(y => y.PartIsDeleted == false).

My question is what is the syntax for something like this

SELECT * FROM ListOfCars cars WHERE  cars.ListOfParts.PartIsDeleted = false
like image 394
user3770093 Avatar asked Dec 10 '25 23:12

user3770093


1 Answers

There are 3 easy combinations in using Any and All.

  1. Any
  2. All
  3. None

Be crystal clear which you do want.


At least one of ListofParts is not deleted.

ListOfCars.Where(x => x.ListofParts.Any(y => y.PartIsDeleted == false));

All of ListofParts are not deleted

ListOfCars.Where(x => x.ListofParts.All(y => y.PartIsDeleted == false));

None of ListofParts is deleted

ListOfCars.Where(x => x.ListofParts.Any(y => y.PartIsDeleted) == false);
like image 176
Tommy Avatar answered Dec 13 '25 13:12

Tommy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!