简体   繁体   English

从列表中选择特定项目 <generic> 通过其变量之一

[英]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 .... 我有一个List<> Generic .generic类内部有变量,例如variable1variable2variable3 ....

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 我想创建一个泛型类的新实例,其值来自此列表中的特定项,其中泛型的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 First()返回列表中的第一项,如果找不到则抛出异常
  • FirstOrDefault() Returns the first item in the list or default value FirstOrDefault()返回列表中的第一项或默认值
  • 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 Single()返回列表中的唯一项目,如果一个以上的项目满足条件或没有一个条件满足条件,则将引发异常
  • 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 SingleOrDefault()返回列表中的唯一项目,如果满足标准的项目超过一个,则将引发异常,如果未找到任何异常,则返回默认值
  • Last() Returns the last item in the list, or throw an exception of none found Last()返回列表中的最后一项,或者引发未找到的异常
  • LastOrDefault() Returns the last item in the list or default value LastOrDefault()返回列表中的最后一项或默认值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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