-
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
-
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!
-
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
-
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
-
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!
-
 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
-
 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.
-
Your probably right I just need to get used to the LINQ syntax.
...joe
-
 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.
-
I did some simple timing tests and the first method is consistantly about 30% faster than embedding the subquery.
...joe
Similar Threads
-
By ASP learner in forum ASP.NET
Replies: 5
Last Post: 10-08-2002, 07:17 PM
-
By NickRick in forum VB Classic
Replies: 1
Last Post: 09-18-2002, 01:43 PM
-
Replies: 2
Last Post: 03-28-2002, 08:14 AM
-
By melvin ng in forum VB Classic
Replies: 1
Last Post: 11-10-2000, 04:46 AM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|