none
LINQ QUERY TO SQL QUERY using DbSet i.e Cofe First Entity Framework. RRS feed

  • Question

  • Hello people,

    Here am trying to Get Sql Query out of linq query using DbContext and Dbset.

    And my observation is when I perform following query 

    Learning.Data.DataContext db = new Data.DataContext();
    var query = db.Employees.Where("Id == @0", Guid.Empty);

    The query.ToString() gives me which is fine

    SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[CityId] AS [CityId]
    FROM [dbo].[Employees] AS [Extent1]
    WHERE cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier) = [Extent1].[Id]

    But when I use any variables I get query with some parameters which is not sql compatible i.e

    string name = "Test"
    Learning.Data.DataContext db = new Data.DataContext();
    var query = db.Employees.Where("Id == @0", Guid.Empty);
    query =query.Where(t => t.Contacts.Any(l=>l.Name == name));;

    i will get query.ToString() as follows

    SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[CityId] AS [CityId]
    FROM [dbo].[Employees] AS [Extent1]
    WHERE (cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier) = [Extent1].[Id]) AND ( EXISTS (SELECT 
    	1 AS [C1]
    	FROM [dbo].[Contacts] AS [Extent2]
    	WHERE ([Extent1].[Id] = [Extent2].[EmployeeId]) AND ([Extent2].[Name] = @p__linq__0)

    We have tried with getting the internal query and converting it to ObjectQuery and tried with ToTraceString as well. But the result is still the same.

    Please help us with the solution.

    Regards,

    Naga Sreenivas Gupta V

    Monday, May 6, 2013 3:56 AM

All replies

  • Hi Naga,

    I would like to know how you are using Where() extension method to query on DbSet, does that come from dynamic LINQ library?

    Do you get the expected result with this query or do you have any other concerns?

    Any updates are appreciated.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 8, 2013 7:42 AM
    Moderator