具有多种条件的数据选择 [英] Datatable select with multiple conditions
问题描述
目标:要查找列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屋!