使用LINQ过滤数据集 [英] Filter a Dataset using LINQ
问题描述
我有一个这样的数据集,只有一行
保健区:
___________________
强度
钠
谷物
灵活性
强度
吸烟
压力
睡眠计划
有氧运动
少坐
餐食
部分
高脂
我必须从数据集中删除与数组dataobject中的数据匹配的行,例如Example
I have a dataset like this with just one row
HealtArea:
___________________
Strenth
Sodium
Grains
Flexibility
Strength
Smoking
Stress
Sleep Plan
Cardio
Sit Less
Meals
Portions
High-Fat
I have to remove from the dataset the rows that match data comming in a array dataobject, say for Example
<pre>MyHealth[] healtharray;
healtharray[0].areaname = 'Sodium'
healtharray[1].areaname = Smoking'
healtharray可以返回任意数量的项目. 2只是一个例子
LINQ可以用于将此数组作为过滤器应用于数据集,以仅获取与此数组项不匹配的列表吗?
healtharray can return any number of items. 2 is just an example
Can LINQ be used to apply this array as a filter on the dataset to only get the list that does not match this array items?
推荐答案
我不知道关于LINQ的方法,但是一个简单的解决方案是将数据集转换为数据视图,从数组中构建一个漂亮的过滤器,然后使用DataView.RowFilter获得所需的结果
I don''t know about LINQ but a simple solution is to convert your dataset to a dataview, build a nice filter from your array and use DataView.RowFilter to get the desired results
如果要在客户端上进行过滤方面,您可以创建一个HashSet<string>
来存储过滤条件.
然后在Where
(LINQ)扩展方法中使用该方法,或者在where
子句中使用该方法.
例如
If you want to filter on client side, you could create aHashSet<string>
to store the filter criterion.
Then use that in aWhere
(LINQ) extension method or analogous in awhere
clause.
E.g.
HashSet<string> exclude = new HaseSet<string>()
{ "..."
, "..."
, "..."
...
};
...
var filteredQuery = myCollection.Where(record => !exclude.Contains(record.FieldToFilter));
...
foreach(var record in filteredQuery)
{
...
}
干杯
安迪
Cheers
Andi
这篇关于使用LINQ过滤数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!