How do you return a specific type in a Linq query? I know you can use ToList() to return a list of specific objects but how do you return a non list?
MyObj x = from x in list where x.id == 99 select x;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Alternative
IEnumerablemethods you can use to return a single item:list.Single(i=>i.id == 99): throws an exception if no matches are found or multiple matches are found.list.SingleOrDefault(i=>i.id == 99): returns null if no matches are found, throws an exception if multiple matches are found.list.First(i=>i.id == 99): throws an exception if no matches are found. If multiple matches are found, returns the first item in the list.list.FirstOrDefault(i=>i.id == 99): returns null if no matches are found. If multiple matches are found, returns the first item in the list.