Rio Izz Well
Rio Izz Well

Reputation: 11

How to use linq query to get value in datatable

That consist of 2 columns: roomType and no rooms

So I want to get no rooms value from room type that i have.

In SQL its look like this:
SELECT no_rooms from table name where roomtype = 'deluxe'
Result: 2

How to access that in LINQ query and store that value as int datatype?

I only know this code

string[] tableName = table.AsEnumerable()
    .Select(s => s.Field<string>("NoRooms"))
    .ToArray<string>()
    .Where(?idont_know_the_query));

Upvotes: 0

Views: 2591

Answers (3)

Suraj Singh
Suraj Singh

Reputation: 4059

var NoOfRooms= tablename.where(x=>x.roomType=="deluxe").ToList();
   int total = NoOfRooms.count();

Upvotes: 0

Kiran Hegde
Kiran Hegde

Reputation: 3681

Here is just another way of retriving the data rows, assuming that table in your example is a DataTable

string expression = string.Format("roomtype='{0}'","deluxe");
var rows = dt.Select(expression);
var strRoomNumber = rows.Select(r=>r.Field<string>("roomNumber")).FirstOrDefault();
int no_rooms;
int.TryParse(strRoomNumber,out no_rooms);

This will return you the first no of rooms for the first matching record

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222532

var results = from myRow in table.AsEnumerable()
where myRow.Field<String>("roomtype ") == "deluxe"  
select myRow;

Upvotes: 1

Related Questions