c# - LINQ - query does not return an object from a list of objects -
i have list of objects , need execute linq query on find specific object.
class myclass { int id; int someotheridbutnotunique; } var ls = myobjectlist.where(x => x.id==specificid && x.someotheridbutnotunique == someotherspecificid) .firstordefault();
but query not return myclass object. , also, how should change query list of myclass objects fulfill given condition. @ same time, know if there linq tutorial can start scratch.
linq extension methods take predicates filter list by. where
, first
, firstordefault
, single
, singleordefault
(to name few) take same predicate.
some lambda expression filter list by.
public class myclass { public int id {get;set;} public int other {get;set;} } // myclasses populated list <-- needs checked. var result = myclasses.firstordefault(x => x.id == specificid && x.other == specificother);
result
should contain either single myclass
instance or null
.
if omit ordefault()
code throw error if can't find instance matches predicate.
if predicate returns several items, first
pick first item. if swap first
single
, predicate returns several items, throw exception.
things check
the list performing query against has list of instances.
the variables
specificid
,specificother
have values exist in list. last thing want wondering why isn't returning anything, when doing asked , reason failing values using query wrong.
Comments
Post a Comment