DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: LINQ criteria question

  1. #1
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353

    LINQ criteria question

    I can generate simple LINQ queries using a listbox as criteria:

    Code:
    Dim Result = From person in db.Persons _
                               Where Person.id = Me.ListBox1.Text _
                               Select Person.Name, Person.Phone
    Is there any way to reference the listbox.SelectedItems or something similar in a multiselect listbox?

    ...joe

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Try this:
    Code:
    Dim Result = From person in db.Persons _
                       Where ListBox1.SelectedItems.Contains(Person.id) _ 
                       Select Person.Name, Person.Phone
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    Throws an error here:

    Code:
    System.NotSupportedException - Method 'Boolean Contains(System.Object)' has no supported translation to SQL
    I notice LINQ does not support the IN SQL clause either.

    ...joe

  4. #4
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    Ok, I got something to work:

    Code:
    Dim Criteria = From item in ListBox1.SelectedItems
    
    Dim Result = From person in db.Persons _
                               Where Criteria.Contains(Person.id) _
                               Select Person.Name, Person.Phone
    I haven't been able to reference the listbox directly in the criteria.

    ...joe

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Hmm. My syntax could be wrong, but Contains should work. See if this helps: http://www.google.com/search?q=linq+sql+%22in+clause%22
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  6. #6
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    Quote Originally Posted by joewmaki
    Ok, I got something to work:

    Code:
    Dim Criteria = From item in ListBox1.SelectedItems
    
    Dim Result = From person in db.Persons _
                               Where Criteria.Contains(Person.id) _
                               Select Person.Name, Person.Phone
    I haven't been able to reference the listbox directly in the criteria.
    This works, but I think the previous syntax is easier to maintain:

    Code:
    Dim Result = From person in db.Persons _
                     Where (From item in ListBox1.SelectedItems).Contains(Person.id) _
                     Select Person.Name, Person.Phone

    ...joe

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by joewmaki
    This works, but I think the previous syntax is easier to maintain:
    I'm curious as to why you think it would be easier to maintain.

    They both look pretty self-explanatory.

  8. #8
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    Your probably right I just need to get used to the LINQ syntax.

    ...joe

  9. #9
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by joewmaki
    Your probably right I just need to get used to the LINQ syntax.
    Yeah, you and me both. But, we will get there.

  10. #10
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    I did some simple timing tests and the first method is consistantly about 30% faster than embedding the subquery.

    ...joe

Similar Threads

  1. Display one record at a time!
    By ASP learner in forum ASP.NET
    Replies: 5
    Last Post: 10-08-2002, 07:17 PM
  2. I need to tie an AccessDB FORM to a REPORT criteria
    By NickRick in forum VB Classic
    Replies: 1
    Last Post: 09-18-2002, 01:43 PM
  3. question on best programming option
    By James in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 08:14 AM
  4. VB,STORED PROC, COM, DTC QUESTION?
    By melvin ng in forum VB Classic
    Replies: 1
    Last Post: 11-10-2000, 04:46 AM
  5. Performance question
    By John in forum VB Classic
    Replies: 2
    Last Post: 04-05-2000, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center