How come Linq to SQl is omitting the where clause? RRS feed

  • Question

  • Hello,

    In the following LINQ

    when I do not pass the positionTitle then the SQL is:

    SELECT  [t0].[positionTitle] ,  [t0].[ActiveId]  FROM [dbo].[RecruitmentSearchView] AS [t0]

    When I pass the positionTitle then the SQL is:

    SELECT  [t0].[positionTitle] ,  [t0].[ActiveId]  FROM [dbo].[RecruitmentSearchView] AS [t0] WHERE [t0].[PositionNumber] LIKE 'senior%'

    using (var dc = GetDataContext()) {
                    var matching = from vw in dc.RecruitmentSearchViews
                                   where (string.IsNullOrEmpty(positionTitle) || vw.PositionTitle.StartsWith(positionTitle))                         
                                   select vw;
                    return matching.ToList();

    My Question:   how come I do not see the where clause in the LINQ to SOL debugger? when I do not pass the positionTitle

    Tuesday, July 6, 2010 8:13 AM


All replies

  • sting.IsNullOrEmpty(positionTitle) evaluates to true, thus short-circuiting the rest of the condition. So before LINQ builds the SQL, it already knows that the where clause is effectively 'where true' so can omit it.

    Tuesday, July 6, 2010 8:29 AM
  • Hi,


    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 


    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.


    Have a nice day!



    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, July 12, 2010 8:07 AM