更新时间:2023-02-05 17:14:08
您可以尝试以下代码:
You can try the below Code:
IList<ResponseDTO> List1 = new List<ResponseDTO>();
ResponseDTO obj = (ResponseDTO)from listObject in List1
where listObject.Code == "1001"
select listObject;
可以使用您的对象来代替ResponseDTO.
希望对您有所帮助.
Instead of ResponseDTO you can use your object.
Hope this helps.
或者,如果您更喜欢扩展方法语法:
Or if you prefer the extension method syntax:
var matching = list1.Where(o => o.code == "1001");
如果您最多知道一个并且想要获取它,那么:
If you know there''s at most one and you want to get it then:
MyObjectType matching = list1.Where(o => o.code == "1001").FirstOrDefault();
如果不存在匹配项,则FirstOrDefault返回null;如果您要求存在匹配项,则使用First(),如果没有匹配项,则抛出异常.
所有这些扩展方法都在IEnumerable< T>上,因此List< MyObjectType>会在列表中显示.会没事的.它们在System.Linq中.
FirstOrDefault returns null if there are no matches, if you require there to be a match then use First() which throws an exception if there are no matches.
All these extension methods are on IEnumerable<T>, so a List<MyObjectType> will be fine. They''re in System.Linq.