LINQ to SQL Saving query results in a variable

For example, I am looking for an identifier for specific individuals, and I want to store this identifier in a local variable or instance variable. How to get query results and save them in int variable with LINQ to SQL? Assuming we have this request

from user in dbo.DoctorsName where doctorsName = "Foo Bar" select DOC_ID; 
+4
source share
3 answers

You can use FirstOrDefault() as follows:

 var results = from user in dbo.DoctorsName where user.doctorsName == "Foo Bar" select user; string personName = results.FirstOrDefault().Name; 
+5
source

This should help.

 var name = dbo.DoctorsName.Where(item => item.doctorsName = "Foo Bar").Select(item => item.Name).FirstOrDefault(); 

If there are no entries for your condition, then using FirstOrDefault () may throw an exception. For this you can try -

 var namelist = dbo.DoctorsName.Where(item => item.doctorsName = "Foo Bar").Select(item => item.Name); If(namelist.Count() > 0) var name = namelist.Fisrt(); 
0
source

Similar to this:

 var ticketDepartment = from t in dt_tickets.AsEnumerable() where t.Field<string>("MID") == mid select new { department = t.Field<string>("Department") }; 

and now you have a variable in ticketDepartment.department

0
source

Source: https://habr.com/ru/post/1397967/


All Articles