Pomster
Pomster

Reputation: 15207

View SQL Generated when using LINQ in ASP.NET Core

I am trying to view the SQL generated in a LINQ Query.

Question GetLatestPoll = 
   _contextService
   .Find<Question>(q => q.Status == PollStatus.Open || (q.Status == PollStatus.Review)
      && ((q.Availability == PollAvailability.All)
      || (q.Availability == PollAvailability.Company && q.User.ClientId == currentUser.ClientId)
      || (q.Availability == PollAvailability.Company && q.User.DepartmentId == currentUser.DepartmentId)) 
      && !q.Answers.Any(a=>a.UserId == currentUser.Id))
   .Include(q => q.Options)
   .OrderByDescending(q => q.CreatedAt)
   .FirstOrDefault();

I wanted to use something like var sql =Context.GetCommand(GetLatestPoll).CommandText;

But the name Context does not exist in Core.

`

Upvotes: 1

Views: 358

Answers (1)

James Curran
James Curran

Reputation: 103585

I can't say for sure about .NET Core, but the LINQ2SQL data context has a Log property, to which you can assign a TextWriter object, which will receive the SQL output.

Upvotes: 1

Related Questions