Results 1 to 9 of 9

Thread: Linq2SQL not executing correctly??

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Linq2SQL not executing correctly??

    Ok ... If i have 2 lists such that:

    TEST CASE ONLY:
    vb Code:
    1. Dim List1 = {1, 2, 3}
    2. Dim List2 = {1, 2, 3}
    3. Dim Test = List1.Where(Function(x) True OrElse (List2.Where(Function(y) MsgBox(x) = MsgBoxResult.Ok)).First <> 1)

    The message box should NEVER be shown because the OrElse will mean that it will short circuit when the left side is true...
    ...so by the same logic:
    vb Code:
    1. ds = Database.PersonNotes.Where(Function(x) True OrElse (x.PersonNoteCategories.Where(Function(y) MsgBox("asd") = MsgBoxResult.Ok).FirstOrDefault IsNot Nothing))
    ... the message box should also not be shown here

    however this is NOT the case...

    Any way around this?
    Thanks,
    Kris

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Linq2SQL not executing correctly??

    I would guess that what's actually happening is that that Lambda expression is being executed in order to produce a value that can then be passed to SQL Server. Remember that LINQ syntax is something that sits on top of a LINQ provider and each provider will be implemented differently. LINQ to SQL has to map to a T-SQL query and there's no way that a MsgBox call is being executed by the database.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Linq2SQL not executing correctly??

    To test my theory, run SQL Profiler while that query is executed and see what SQL code is actually executed by the database.

  4. #4

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL not executing correctly??

    The message box was just a test ... it does the same regardless ... it runs the whole thing because it is ALOT slower ... in fact ... so much that this seems much better speed wise:

    vb Code:
    1. 'GENERIC
    2. Dim SearchText = trim(txtSearch.Text)
    3. Dim SearchFilter = SearchText <> ""
    4. Dim IncludeDeleted = tsbIncludeDeleted.Checked
    5.  
    6. 'ORIGIONAL
    7. Dim ds = Database.PersonProgNotes.Where(Function(x) x.PersonID = SelectedPersonID AndAlso (IncludeDeleted OrElse x.DeletedOn.HasValue = False) AndAlso (SearchFilter = False OrElse (If(tsiSearchNotes.Checked, x.ProgressNote.Contains(SearchText), False) OrElse If(tsiSearchCategories.Checked, x.PersonProgNoteCategories.Where(Function(y) y.Category.Contains(SearchText)).FirstOrDefault IsNot Nothing, False)))).OrderByDescending(Function(x) x.CreatedOn)
    8.  
    9. 'TO WORK AROUND ISSUE
    10. Dim ds = Database.PersonProgNotes.Where(Function(x) x.PersonID = SelectedPersonID AndAlso (IncludeDeleted OrElse x.DeletedOn.HasValue = False))
    11. If SearchFilter Then
    12.     If tsiSearchCategories.Checked AndAlso tsiSearchNotes.Checked Then
    13.         ds = ds.Where(Function(x) x.ProgressNote.Contains(txtSearch.Text) OrElse x.PersonProgNoteCategories.Where(Function(y) y.Category.Contains(txtSearch.Text)).FirstOrDefault IsNot Nothing)
    14.     ElseIf tsiSearchCategories.Checked Then
    15.         ds = ds.Where(Function(x) x.PersonProgNoteCategories.Where(Function(y) y.Category.Contains(txtSearch.Text)).FirstOrDefault IsNot Nothing)
    16.     ElseIf tsiSearchNotes.Checked Then
    17.         ds = ds.Where(Function(x) x.ProgressNote.Contains(txtSearch.Text))
    18.     End If
    19. End If

    This is ALOT better for cases when the user is searching through Categories Xor Notes... but still there seems no way of getting round searching for both @ the same time.. as you can see by this line:
    ds = ds.Where(Function(x) x.ProgressNote.Contains(txtSearch.Text) OrElse x.PersonProgNoteCategories.Where(Function(y) y.Category.Contains(txtSearch.Text)).FirstOrDefault IsNot Nothing)

    So basically it will still execute the 2nd bit of the above line even if the Note contains the value i am looking for ... but at least it now only does it when you have specified to search through both.

    Kris

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Linq2SQL not executing correctly??

    As I said, LINQ to SQL gets mapped to T-SQL. There is no ORELSE in T-SQL so both Or and OrElse get mapped to the same T-SQL OR operator. It's important to remember that not all LINQ is LINQ to Objects. You have to consider the implementation of the underlying provider.

  6. #6

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL not executing correctly??

    OK ... is there a way to manually feed a query and specify it is of type Database.PersonProgNotes and have it use the query i gave it instead, and have it work as-if i called it the same way as i was?

    Thanks,
    Kris

  7. #7

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL not executing correctly??

    Quote Originally Posted by i00 View Post
    OK ... is there a way to manually feed a query and specify it is of type Database.PersonProgNotes and have it use the query i gave it instead, and have it work as-if i called it the same way as i was?

    Thanks,
    Kris
    Hrm ... it looks like you kind of can ... just found this:
    Dim something = Database.ExecuteQuery(Of Person)("SELECT * FROM person", "" )
    ... but it seems a little different ... if I try to seek through it several times I get:

    The query results cannot be enumerated more than once.

    Kris

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Linq2SQL not executing correctly??

    Quote Originally Posted by i00 View Post
    Hrm ... it looks like you kind of can ... just found this:
    Dim something = Database.ExecuteQuery(Of Person)("SELECT * FROM person", "" )
    ... but it seems a little different ... if I try to seek through it several times I get:

    The query results cannot be enumerated more than once.

    Kris
    It might use a data reader under the hood.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Linq2SQL not executing correctly??

    If I remember right, I think that's correct.

    Also one of the things that affects it, is the deferred execution of the LINQ. That can provide some interesting side effects. I wonder if that's what's going on here... the initial LINQ isn't executing, and when it finally does, it still needs to execute the inner LINQ (because it too was deferred) which then causes the strange behavior. In the original post, where you used the List of Integers, because all values are known, the execution isn't deferred, but optimized immediately, allowing your logic to operate as expected.


    Pure conjecture though.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width