Need to check if a list contains an item with a property value of X.
Been using FirstOrDefault and comparing to null:
searchItems.FirstOrDefault(si => si.ID == 99) == null
Is there better way to do this?
I cannot get past syntax errors on Contains. Thanks.
LINQ Contains is quantifier operator. In LINQ Contains it also checks with the data sources, to check whether the collection of lists contains their desired element or not, and then it returns the result as either true or false based on the expected outcomes of the result.
Select(x => new { x, count = x. tags. Count(tag => list. Contains(tag)) }) .
If you do not call ToList() and your final mapping to the DTO type, you can add Where clauses as you go, and build the results at the end: var query = from u in DataContext. Users where u. Division == strUserDiv && u.
var fruit = ListOfFruits. FirstOrDefault(x => x.Name == "Apple"); if (fruit != null) { return fruit.ID; } return 0; This is not the only road to Rome, you can also use Single(), SingleOrDefault() or First().
You can use the Any method
searchItems.Any(si => si.ID == 99)
There are probably a few ways to do this, here's another one:
bool any = searchItems.Any(si => si.ID == 99);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With