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).

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
user1776590
  • 135
  • 5
  • 14

2 Answers2

2

Yeah you can query it just like normal Enumerable collection -

Assuming, you want to get all rows for particular column name "Id" to be equal to 5, you can query it like this -

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

This will return all rows whose columns Id value is 5.

You can query similarly for other dataTypes, say you want to get all rows with Name set to TestName like this -

sqlData.AsEnumerable().Where(data => data.Field<string>("Name") == "TestName");
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
0

Try this

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

// Query will return data records whose id = 10 and name = 'somename'.

Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62