Get SQL query from LINQ to SQL?
In debugger hover mouse over commiteeMember
variable - it will show generated SQL query:
This is what ToString()
returns for query. You can get same generated SQL query manually by calling ToString:
string sql = committeeMember.ToString();
This overridden method internally calls ObjectQuery.ToTraceString()
which returns commands that will run on data source.
Also you can use SQL Profiler or Entity Framework Profiler to see which SQL query was executed.
Incidentally, LINQ to SQL is not entity framework. If the former, you can set [yourDataContext].Log = Console.Out
(or some other stream writer) and get the query.
In the query you have, consider writing it as follows to be able to do the ToString
trick:
var committeeMemberQuery =
db.Committee_Member.*WHERE*
(x =>
x.Customer_Number == activity.Contact.Number
&& x.Position_Start_Date.Value.Year == activity.EndDate
&& x.Committee_Id == activity.Committee.Id && x.Cancelled != 1
);
var committeeMember = committeeMemberQuery.FirstOrDefault();
Now you can do committeeMemberQuery.ToString()
however you will not get parameter info (you will with DataContext.Log = Console.Out
but again, that's not Entity Framework, it's LINQ to SQL.