LINQ过滤列表对象

时间:2011-03-01 12:37:34

标签: vb.net linq list

我有list对象,我需要在VB.NET中使用LINQ检查逗号分隔字符串中的id,如下所示:

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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

未经测试,但我应该这样做。

答案 2 :(得分:0)

如果在linq中使用字符串之前拆分字符串,则代码完全正常 在C#中你会这样做:

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

也许你可以理解这一点,把它翻译成VB

答案 3 :(得分:0)

使用C#,

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

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

使用VB.NET,

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

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

参考Creating SQL IN Queries using LINQ