如何数据表.Select()过滤数字

时间:2017-07-27 09:29:45

标签: c# .net datatable

我想过滤我的数据表。我使用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"]) +"'" 

2 个答案:

答案 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"]);