Abohy
Abohy

Reputation: 35

How to select values from datatable based on another datatable using DataRow list

I have 2 datatable

I want to filter result in DataTable dtPross (the Second) based on data in datatable dtSeats (the First)

I use this code:

DataTable dtSeats = new DataTable();
dtSeats = "select SitNum,Num from dbo.Table1"
SitNumList = (from row in dtSeats.AsEnumerable() 
             select Convert.ToInt32(row["SitNum"])).ToList();

When I use this code to filter the data based on Datarow list

DataTable dtPross = "select SitNum from dbo.Table2 where SitNum in (" + SitNumList.ToList() + ")";

I got this error

SitNum in (System.Collections.Generic.List`1[System.Int32])

Did i miss something?

Thanks in advance.

Upvotes: 0

Views: 166

Answers (1)

Prasad Telkikar
Prasad Telkikar

Reputation: 16079

Yes, you missed to convert List<int> to comma seperated elements string.

Try below code

//Below code will convert List<int> SitNumList = new List<int>(){1, 2, 3, 4} to string "1, 2, 3, 4"
string siteNums = string.Join(", ", SitNumList);
DataTable dtPross = "select SitNum from dbo.Table2 where SitNum in (" + siteNums  + ")";

Upvotes: 1

Related Questions