vb.net 使用 LINQ 对 DataTable 进行 LIKE 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19889473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LIKE query on DataTable using LINQ
提问by Mad Dog Tannen
Ive just started learning Linq. Ive just created a sample test like this.
我刚开始学习 Linq。我刚刚创建了一个这样的示例测试。
Dim dt As New DataTable
Dim dc As New DataColumn
dc.ColumnName = "Test"
dt.Columns.Add(dc)
dt.Rows.Add("Test")
dt.Rows.Add("One test")
dt.Rows.Add("Second test")
Dim results = From myRow In dt.AsEnumerable
Where myRow("Test") = "Test"
Select myRow
For Each Row In results
Console.WriteLine(Row.Item(0).ToString())
Next
This returns the first row in the iteration.
这将返回迭代中的第一行。
But what if i want to use a LIKE operator using %? I cant get it to work.
但是,如果我想使用 % 来使用 LIKE 运算符怎么办?我无法让它工作。
Ive tried
我试过了
Where myRow("Test") LIKE "Test%"
回答by James
Sounds to me like you want to use StartsWithi.e.
听起来像你想使用StartsWith即
Dim results = From myRow In dt.AsEnumerable
Where myRow("Test").StartsWith("Test")
Select myRow
Containswill match anywhere in the string where as StartsWithwill only match if it's at the beginning of the string (same logic as Test%).
Contains将匹配字符串中的任何位置,其中 asStartsWith仅匹配位于字符串的开头(与 相同的逻辑Test%)。
回答by dev hedgehog
You migh be looking for contains.
您可能正在寻找包含。
Take a look at this:
看看这个:
var query = from mem in context.Member
where mem.LastName.Contains("xxx")
orderby mem.LastName, mem.FirstName
select new
{
FirstName = mem.FirstName,
LastName = mem.LastName,
};
You can also use .StartsWith() or .EndsWith().
您还可以使用 .StartsWith() 或 .EndsWith()。
回答by Alberto
You can use the contains method:
您可以使用 contains 方法:
Dim results = From myRow In dt.AsEnumerable
Where myRow("Test").Contains("Test")
Select myRow

