我想过滤我的数据表。我使用datatable.Select()
进行过滤,但其中一个条件UnitPrice
始终与数据不匹配。此列UnitPrice
是数字,并且有一个小数点,如0.1750。
如何修改我的代码?感谢。
我试试:
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() + "'"
再试一次:
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"]) +"'"
答案 0 :(得分:1)
您需要删除数字And UnitPrice = '" +detailrow["UnitPrice"].ToString() +"'"
。
尝试更改:
And UnitPrice = " +detailrow["UnitPrice"].ToString()
为:
{{1}}
答案 1 :(得分:1)
您可以使用UnitPrice
在DataTable Select中将String
作为' (single quotes)
传递。
我认为除了PatternCode
之外,如果PatternCode
有字符串文字,则其他所有其他内容都不需要单引号来过滤记录。
请尝试以下代码段:
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"]);