Victor Lin
Victor Lin

Reputation: 41

How to datatable.Select() filter numeric

I want to filter my datatable. I use datatable.Select() to filter but one of the condition UnitPrice always not match the data. This column UnitPrice is numeric and has a decimal point, like 0.1750.

How to modify my code? thanks.

I try:

var result = 
 dt_Excel.Select(@" ID = '" + detailrow["Details_MasterID"].ToString() + "'"
  + " And PatternCode = '" + detailrow["PatternCode"].ToString() + "'"
  + " And StyleID  = '" + detailrow["StyleID"].ToString() + "'"
  + " And BrandID  = '" + detailrow["BrandID"].ToString() + "'"
  + " And SeasonID  = '" + detailrow["SeasonID"].ToString() + "'"
  + " And UnitPrice  = '" +detailrow["UnitPrice"].ToString() + "'"

try again:

var result = 
 dt_Excel.Select(@" ID = '" + detailrow["Details_MasterID"].ToString() + "'"
  + " And PatternCode = '" + detailrow["PatternCode"].ToString() + "'"
  + " And StyleID  = '" + detailrow["StyleID"].ToString() + "'"
  + " And BrandID  = '" + detailrow["BrandID"].ToString() + "'"
  + " And SeasonID  = '" + detailrow["SeasonID"].ToString() + "'"
  + " And UnitPrice  = '" +Convert.ToDecimal( detailrow["UnitPrice"]) +"'" 

Upvotes: 0

Views: 732

Answers (2)

Balagurunathan Marimuthu
Balagurunathan Marimuthu

Reputation: 2978

You were suppose to pass the UnitPrice as String in DataTable Select using ' (single quotes).

I believe that except PatternCode all other doesn't require single quotes to filter a record if PatternCode has string literals.

Try the below code snippet:

var result = dt_Excel.Select(@" ID = " + detailrow["Details_MasterID"] + "'"
                                         + " And PatternCode = '" + detailrow["PatternCode"] + "'"
                                         + " And StyleID  = " + detailrow["StyleID"]
                                         + " And BrandID  = " + detailrow["BrandID"]
                                         + " And SeasonID  = " + detailrow["SeasonID"]
                                         + " And UnitPrice  = " + detailrow["UnitPrice"]);

Upvotes: 1

mjwills
mjwills

Reputation: 23898

You need to remove the ' for numerics.

Try changing:

And UnitPrice  = '" +detailrow["UnitPrice"].ToString() +"'" 

to:

And UnitPrice  = " +detailrow["UnitPrice"].ToString()

Upvotes: 1

Related Questions