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