Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL Storing query results in a variable

For example I am searching for a specific persons ID and I want to store that ID to a local variable or instance variable. How do I retrieve Query results and stores them in a int Variable with LINQ to SQL? Assuming we have this query

from user in dbo.DoctorsName
where doctorsName  = "Foo Bar"
select DOC_ID;
like image 566
user962206 Avatar asked Feb 23 '12 06:02

user962206


People also ask

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

Is LINQ to SQL still used?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

How do I get SQL query from LINQ in Visual Studio?

You can get same generated SQL query manually by calling ToString: string sql = committeeMember. ToString();


1 Answers

You can use FirstOrDefault() like this:

var results = from user in dbo.DoctorsName
              where user.doctorsName  == "Foo Bar"
              select user;

string personName = results.FirstOrDefault().Name;
like image 194
Mahmoud Gamal Avatar answered Sep 30 '22 13:09

Mahmoud Gamal