0

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)

4 Answers 4

7
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
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.