How to get single value from datatable using Linq

Hi Guys,

I want to get column 2 value by using column 1 value from datatable using linq
example: consider the below datatable
ID Age
768341 45
435689 27
456170 38

i will be knowing ID, using ID i need to get there age,
anyone can help me with this?

1 Like

Hi @anishakotian400

There are multiple ways in which it can be done a couple of them are

DT1.Select("[EmpID]=1").CopyToDataTable

or

DT1.AsEnumerable().Where(function(r) r("EmpID").Equals("1")).CopyToDataTable

or

(From p In DT1.AsEnumerable()
Where
p("EmpID").Equals("1")
Select p
).CopyToDataTable

or you can simply use filter datatable activity

Thanks,
Prankur

2 Likes

Hi,

DT1.AsEnumerable().Where(function(r) r("EmpID").Equals("1")).CopyToDataTable().First().Item("Age").ToString

2 Likes
dtData.AsEnumerable().Where(function(x) x("EmpID").toString.Trim.Equals("12345")).Select(Function (x) x("Age").FirstOrDefault

handling not found case

We do not use CoptToDataTable when we can expect also an empty filter result. In such case CopyToDataTable will throw an exception. FirstOrDefault will return null in the case no matching row was found

4 Likes

@anishakotian400 Try this

Assign Str_Variable = DT2.AsEnumerable().Where(function(r) r(“ID”).Equals(“768341”)).CopyToDataTable.Rows(0)(1).ToString

This will give required Age value 45

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.