1

I am currently using the code below and am wondering if it would be possible to query based on an entry value. At the moment it returns the number of rows that have been defined.

var q = sqlData.AsEnumerable().Take(2);

This data comes in from a database and is imputed into the table but at the moment it only returns database data into the datatable and allows me to select the first two rows and I was wondering if I can query the data table so that I can get the rows that I require based on an index in the actual table itself (e.g. in the table I find five rows and query this information out).

4

2 回答 2

2

是的,您可以像普通的 Enumerable 集合一样查询它 -

假设,您希望特定列名的所有行都"Id"等于5,您可以像这样查询它 -

sqlData.AsEnumerable().Where(data => data.Field<int>("Id") == 5);

这将返回列Id值为 的所有行5

您可以类似地查询其他数据类型,假设您想获取所有Name设置为TestName这样的行 -

sqlData.AsEnumerable().Where(data => data.Field<string>("Name") == "TestName");
于 2012-11-13T16:53:20.450 回答
0

尝试这个

var q = sqlData.AsEnumerable()
               .FindAll(data => data.Field<int>("Id") == 10 
                     && data.Field<string>("Name").Equals("SomeName"));

// 查询将返回数据记录,其id = 10name = 'somename'

于 2012-11-13T16:56:08.873 回答