Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have list of object list1 ,from this list i want to select an object say "obj" with
obj.code="1001"
Please suggest Linq query for this
Posted

You can try the below Code:

XML
IList<ResponseDTO> List1 = new List<ResponseDTO>();
ResponseDTO obj = (ResponseDTO)from listObject in List1
                         where listObject.Code == "1001"
                         select listObject;


Instead of ResponseDTO you can use your object.

Hope this helps.
 
Share this answer
 
Comments
VJ Reddy 30-May-12 8:53am    
Good answer. 5!
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 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.
 
Share this answer
 
Comments
VJ Reddy 30-May-12 8:53am    
Good answer. 5!
rajin kp 8-Jun-12 0:34am    
Thanks BobJanova.
Plz suggest some links for Linq

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900