[英]Use C# DataView.RowFilter Property on a function's argument of string type
Is there a way to use the Rowfilter
property of DataView
on the following string
variable? 是否可以在以下
string
变量上使用DataView
的Rowfilter
属性?
public DataTable FilterByLatestDate(DataTable tbl, string date_asof)
{
DataView latest = tbl.DefaultView;
//latest.RowFilter = "[date]= '" + date_asof + "'" ; //
//latest.RowFilter = string.Format("[date]= '{0}'", date_asof); // alternative to messing around with quotes
DataTable last_tbl = latest.ToTable();
return last_tbl;
}
Ps: I am only interested in suggestions involving .Rowfilter
.Rowfilter
:我只对涉及.Rowfilter
建议.Rowfilter
Best 最好
When you use a Date in a RowFilter the quoting characted to use is the # symbol 当您在RowFilter中使用日期时,使用的引号是#符号
latest.RowFilter = "[date]= #" + date_asof + "#" ;
However the RowFilter property accepts a string and thus when you use a Date for the filter the variable should be expressed in format as "MM/dd/yyyy". 但是,RowFilter属性接受字符串,因此,当您将Date用于过滤器时,变量应以“ MM / dd / yyyy”的格式表示。 So if your string
date_asof
is not in this format then this code could not work correctly. 因此,如果您的字符串
date_asof
不是这种格式,则此代码将无法正常工作。
I suggest you to pass a DateTime to your function and format it with 我建议您将DateTime传递给函数并将其格式化为
public DataTable FilterByLatestDate(DataTable tbl, DateTime date_asof)
{
DataView latest = tbl.DefaultView;
latest.RowFilter = "[date]= #" + date_asof.ToString("MM/dd/yyyy") + "#";
return latest.ToTable();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.