Kaishu
Kaishu

Reputation: 377

Why does dataTable.Select always returning empty result?

var tmpTable = dt.Select("strText IN('" + strZipList + "')");
if (tmpTable.Any())
{
dt = tmpTable.CopyToDataTable();
}

dt = DataTable and strText is of type string strZipList = string('1','2',....etc)

Edited: DataTable structure created as:

private DataTable CreateListTabel()
{
    DataTable _dtList = new DataTable();
    DataColumn dc1 = new DataColumn("strText", typeof(String));
    DataColumn dc2 = new DataColumn("strValue", typeof(String));
    DataColumn dc3 = new DataColumn("isSelected", typeof(Boolean));
    DataColumn dc5 = new DataColumn("intSrNo", typeof(Int32));
    DataColumn dc6 = new DataColumn("intID", typeof(Int32));
    DataColumn dc7 = new DataColumn("isExcluded", typeof(Boolean));
    DataColumn dc8 = new DataColumn("isDisabled", typeof(Boolean));
    _dtList.Columns.Add(dc1);
    _dtList.Columns.Add(dc2);
    _dtList.Columns.Add(dc3);
    _dtList.Columns.Add(dc5);
    _dtList.Columns.Add(dc6);
    _dtList.Columns.Add(dc7);
    _dtList.Columns.Add(dc8);
    _dtList.AcceptChanges();
    return _dtList;
}

Where does it going wrong?

Upvotes: 0

Views: 624

Answers (2)

Nino
Nino

Reputation: 7095

Lets say that your search list is List<string>. In that case, you could search like in this example (sorry, I wrote an example before you explained your table structure).

also, you can search it using LINQ, as shown in another line In case you're selecting using string variable, look at third line. Hope this helps

DataTable table = new DataTable();
table.Columns.Add("column", typeof(string));

table.Rows.Add("1");
table.Rows.Add("2");
table.Rows.Add("2");
table.Rows.Add("3");
table.Rows.Add("4");
table.Rows.Add("4");

List<string> searchList = new List<string> { "1", "2" };
string searchString = "1, 2";

//DataTable.Select(string) example
var filteredTable = table.Select("column in ("  + string.Join(", ", searchList.ToArray()) + ")");
//LINQ
var filteredTable2 = table.AsEnumerable().Where(row => searchList.Contains(row.Field<string>("column"))).CopyToDataTable();
//using string for search
var filteredTable3 = table.Select("column in (" + searchString + ")");

Upvotes: 1

Adam Calvet Bohl
Adam Calvet Bohl

Reputation: 1079

Can you be more specific in the strZipList string format?

strZipList = string('1,','2',....etc)

It should be like '1, 2, 3 ...', not like '1', '2', '3'...

OOPS! I mean: It should be like '1', '2', '3' ...', not like '1,','2'!

By the way, be sure to check your strings are EXACTLY the same when comaring them (no extra spaces, etc..). '1' is not the same as ' 1' (look at extra spaces...)

Upvotes: 0

Related Questions