LINQ Query: How to simulate the behavior of the IN statment

In Linq doesn’t exists the IN statment. In fact, the ability to filter the values ​​for multiple occurrences not missing, it writes only in a different way, using a different approach. In this case, if you want to filter the results of a LINQ query, showing more than a filter value, it must build a collection of values ​​to extract and use the Contains method of the collection itself. In other words if I want to extract all of a customers named "Rossi" and "Bianchi", I could just use this approach:

 

 Sub Main()         
Dim Coll As New List(Of Customer)         
Coll.Add(New Customer With {.ID = 1, .FirstName = "Franco", .LastName = "Rossi"})         
Coll.Add(New Customer With {.ID = 2, .FirstName = "Gianni", .LastName = "Verdi"})         
Coll.Add(New Customer With {.ID = 3, .FirstName = "Lorenzo", .LastName = "Bianchi"})         
Coll.Add(New Customer With {.ID = 4, .FirstName = "Gianmaria", .LastName = "Rossi"})         
Coll.Add(New Customer With {.ID = 5, .FirstName = "Luca", .LastName = "Bianchi"})         
Coll.Add(New Customer With {.ID = 6, .FirstName = "Vittorio", .LastName = "Rossi"})         
Coll.Add(New Customer With {.ID = 7, .FirstName = "Filippo", .LastName = "Verdi"})         
Dim FilterValue() As String = {"Rossi", "Bianchi"}         
Dim Query = Coll.Where(Function(t) FilterValue.Contains(t.LastName))
 
        For Each c As Customer In Query             
 HTH
Alberto

Print | posted on giovedì 7 aprile 2011 18.37

Feedback

No comments posted yet.

Your comment:





 
Please add 1 and 4 and type the answer here:

Copyright © Alberto De Luca

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski