linq item get without foreach in selected new keyword

This question already has an answer here:

  • Linq code to select one item 8 answers

i wants to get a data using linq to SQL. I used for each like this is there is another way to get query data without for each .basically i wants to get from query without using for each loop because i know there is one object no need to used loop for it.

var query=from d in db.table where select new {}; foreach (var item in query) { string; }

-------------Problems Reply------------

Looks like you want only the first name, you don't need an anoymous type here, you can just use FirstOrDefault like this:

db.table.Where(d => == 2).Select(d =>;

This will return the name of the first record with id 2 or return null if there is no such record.

If you want all names use ToList instead of FirstOrDefault

You can use FirstOrDefault with your code like this (if is a string):

var query=(from d in db.table

Category:c# Views:2 Time:2017-09-10
Tags: linq

Related post

Copyright (C), All Rights Reserved.

processed in 0.193 (s). 10 q(s)