Linq搜索数据集中的值 [英] Linq to search the value in dataset

查看:66
本文介绍了Linq搜索数据集中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,我想知道某个列中是否存在某个值的部分。



我尝试了什么:



  Dim  str  as   string  =   Apple  
Dim 包含作为 Boolean = ds.Tables( result)。AsEnumerable()。Any(< span class =code-keyword> Function (row)str = row.Field( of [ String ])( COL1))



这给了我真假,这就是我想要的。

但如果我的专栏有



Applea

AppleB



它应该返回true。这意味着它必须搜索包含而不是确切的单词。

解决方案

那么使用包含方法?

  Dim  str  as   string  =   Apple 
Dim 包含 As Boolean = ds.Tables( result)。AsEnumerable()。Any( Function (row)row.Field( Of [ String ])(< span class =code-string> COL1)。包含(str))



或者,如果你想要一个不区分大小写的比较:

  Dim  str  as   string  =   Apple 
Dim 包含 As < span class =code-keyword> Boolean = ds.Tables( result ).AsEnumerable()。any( Function (row)row.Field( of [字符串])( COL1)。IndexOf(str ,StringComparison.OrdinalIgnoreCase)<> -1)


实际上有一个字符串方法包含(..):-)



String.Contains Method(String)(System) [ ^ ]



  Dim 包含作为 布尔 = ds.Tables(  result)。AsEnumerable()。Any( Function (row)row.Field(  [ String ])(  COL1 )。包含(STR))


I have a dataset and I would like to know if there is a part of value in certain column exists.

What I have tried:

Dim str as string = "Apple"
Dim contains As Boolean = ds.Tables("result").AsEnumerable().Any(Function(row) str= row.Field(Of [String])("COL1"))


This gives me true of false which is what I want.
but If my column has

Applea
AppleB

it should return true. Meaning it has to search for "Contains" instead of exact word.

解决方案

So what's wrong with using the Contains method?

Dim str as string = "Apple"
Dim contains As Boolean = ds.Tables("result").AsEnumerable().Any(Function(row) row.Field(Of [String])("COL1").Contains(str))


Or, if you want a case-insensitive comparison:

Dim str as string = "Apple"
Dim contains As Boolean = ds.Tables("result").AsEnumerable().Any(Function(row) row.Field(Of [String])("COL1").IndexOf(str, StringComparison.OrdinalIgnoreCase) <> -1)


There actually is a String-method "Contains(..)" :-)

String.Contains Method (String) (System)[^]

Dim contains As Boolean = ds.Tables("result").AsEnumerable().Any(Function(row) row.Field(Of [String])("COL1").Contains(str))


这篇关于Linq搜索数据集中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆