Victor Lin Victor Lin - 1 year ago 133
C# Question

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"]) +"'"

Answer Source

You need to remove the ' for numerics.

Try changing:

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

to:

And UnitPrice  = " +detailrow["UnitPrice"].ToString()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download