具有多种条件的数据选择 [英] Datatable select with multiple conditions

查看:145
本文介绍了具有多种条件的数据选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4列A,B,C和D列的数据表,以便列A,B和C中的值的特定组合在数据表中是唯一的。



目标:要查找列D的值,请针对列A,B和C的给定值组合。



我猜循环遍历数据行集合。有没有办法使用Datatable.Select()来完成这个?要更具体 - 我可以在选择过滤器中有多个条件,即逻辑AND运算符连接每个列A,B和C的条件。

解决方案

是的, DataTable.Select 方法支持布尔运算符,方法与使用实际SQL语句相同:

  DataRow [] results = table.Select(A ='foo'AND B ='bar'AND C ='baz') ; 

请参阅 DataColumn.Expression在MSDN 中DataTable支持的语法选择方法。 p>

I have a datatable with 4 columns A, B, C and D such that a particular combination of values for column A, B and C is unique in the datatable.

Objective: To find the value of column D, for a given combination of values for column A, B and C.

I guess looping over the set of data rows should do it. Is there a way to use Datatable.Select() to accomplish this? To be more specific - can I have multiple conditions in the select filter i.e. a logical AND operator connecting conditions for each of the columns A, B and C.

解决方案

Yes, the DataTable.Select method supports boolean operators in the same way that you would use them in a "real" SQL statement:

DataRow[] results = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'");

See DataColumn.Expression in MSDN for the syntax supported by DataTable's Select method.

这篇关于具有多种条件的数据选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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