0

how to translate a linq expression for example

var query = (from article in ArticleRepository.GetAll()
            join read in ArticleReadRepository.GetAll() on read.ArticleId equals article.Id
            where article.Id>10
            select new 
            {
                article.Title,
                read.ReadCount
            }).ToList();

into sql string

select article.Title,
       read.ReadCount 
       from ArticleTable as article 
       join ArticleReadTable as read on read.ArticleId = article.Id 
       where article.Id>10

use c# code

1 Answer 1

2

Print query.ToString() and you will see the query formed by the LINQ expression

For example,

var query = from emp in v.Employees
            select emp;
var sqlQuery = query.ToString();
Console.WriteLine(sqlQuery);

The result will be:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name]
FROM [dbo].[Employee] AS [Extent1]
Sign up to request clarification or add additional context in comments.

3 Comments

actually,i want to know the implementing of query.ToString()...ok,i will see the source code for it.
You can retrieve it as a string from query.ToString and fire it as a sql query.
@ReimuLyu Please mark it as an answer if it solved your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.