Retrieve LINQ to sql statement (IQueryable) WITH parameters

前端 未结 4 2059
广开言路
广开言路 2021-02-01 14:16

I\'m trying to figure out if there\'s a way to retrieve the (full) sql statement that gets executed on the database server.
I found something already, but it does not exactl

4条回答
  •  -上瘾入骨i
    2021-02-01 14:49

    You can also see the generated sql query if you have an instance of IQueryable and call the .ToString() method.
    For Example:

    var db = new DbContext();
    IQueryable query = db.Blog.Where(tt=> tt.Id > 100).OrderByDescending(tt=>tt.Id);
    var sqlString = query.ToString();
    Console.WriteLine(sqlString);
    

    This will generate an output of:

    SELECT [Extent1].[Id] AS [Id], 
    [Extent1].[Title] AS [Title], 
    [Extent1].[Author] AS [Author], 
    [Extent1].[Text] AS [Text], 
    [Extent1].[CreatedAt] AS [CreatedAt], 
    [Extent1].[UpdatedAt] AS [UpdatedAt]
    FROM [dbo].[Blogs] AS [Extent1]
    WHERE [Extent1].[Id] > 100
    ORDER BY [Extent1].[Id] DESC
    

提交回复
热议问题