LINQ 过滤列表对象

发布于 2024-10-20 00:51:54 字数 172 浏览 2 评论 0原文

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

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衬遮猫 2024-10-27 00:51:54
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
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
计㈡愣 2024-10-27 00:51:54
dim strId as String = "1,2,3,5,"
dim myList = from objmylist where strId.split(",").Contains(objmylist.id)

未经测试,但我想应该可以解决问题。

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.

感受沵的脚步 2024-10-27 00:51:54

如果您在 linq 中使用字符串之前将其拆分,那么您的代码就完全没问题
在 C# 中你会这样做:

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

也许你可以理解这一点并将其翻译成 VB

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

爱已欠费 2024-10-27 00:51:54

使用 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)

参考 使用 LINQ 创建 SQL IN 查询

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文