skamale
skamale

Reputation: 673

LINQ filtering list objects

I have list object and I need to check id in a comma separated string using LINQ in VB.NET, something like this:

dim strId as String = "1,2,3,5,"
dim myList = from objmylist where objmylist.id in (strId)

Upvotes: 0

Views: 21017

Answers (4)

Harsh Baid
Harsh Baid

Reputation: 7249

Using C#,

int[] productList = new int[] { 1, 2, 3, 4 };

var myProducts = from p in db.Products
                 where productList.Contains(p.ProductID)
                select p;

using VB.NET,

Dim productList As Integer() = New Integer() {1, 2, 3, 4}

Dim myProducts = From p In db.Products Where productList.Contains(p.ProductID)

In Reference from Creating SQL IN Queries using LINQ

Upvotes: 0

Bazzz
Bazzz

Reputation: 26912

Your code is perfectly fine if you split the string before using it in the linq In C# you would do this:

string strIDs = "1,2,3,5,";
string[] arrIDs = strIDs.Split(",");
var myList = objmylist.Where(o => arrIDs.Contains(o.id));

Perhaps you can understand this enough to translate it into VB

Upvotes: 0

Bala R
Bala R

Reputation: 108937

dim strId as String = "1,2,3,5,"
dim IDs as String() = strId.Split(",")
dim myList = from objmylist where IDs.Contains(objmylist.id) 
             select objmylist

Upvotes: 7

Jens
Jens

Reputation: 3299

dim strId as String = "1,2,3,5,"
dim myList = from objmylist where strId.split(",").Contains(objmylist.id)

untested, but should do the trick I guess.

Upvotes: 0

Related Questions