简体   繁体   中英

Select a specific Item from a list<generic> by one of its variable

I have a List<> Generic .The generic class has variables inside such as variable1 , variable2 , variable3 ....

I want to create a new instance of a generic class with values coming from a specific item in this list where generic's variable3 = some value

Thanks :)

GenericClass item = yourList.FirstOrDefault(r=> r.variable3 == "somevalues");

If you only need to select a single item which met the criteria, then you can use,

  • First() Returns the first item in the list, or throw an exception if none found
  • FirstOrDefault() Returns the first item in the list or default value
  • Single() Returns the only item in the list, if more than a single item met the criteria or none met the criteria then an exception would be thrown
  • SingleOrDefault() Returns the only item in the list, if more than a single item met the criteria then an exception would be thrown, if none is found, default value is returned
  • Last() Returns the last item in the list, or throw an exception of none found
  • LastOrDefault() Returns the last item in the list or default value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM