如何使用Linq执行此操作? 我尝试过没有成功
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
DataView view = socialEvents.DefaultView;
view.RowFilter = String.Format(
"Date >= #{0}# AND Date < #{1}#",
Calendar1.SelectedDate.ToShortDateString(),
Calendar1.SelectedDate.AddDays(1).ToShortDateString()
);
if (view.Count > 0)
{
DataGrid1.Visible = true;
DataGrid1.DataSource = view;
DataGrid1.DataBind();
}
else
{
DataGrid1.Visible = false;
}
}
我试试这个,但它不起作用
用户代码未处理SyntaxErrorException表达式 包含无效的名称:[]。
另一次尝试
var rows = socialEvents.Rows.Cast<DataRow>()
.Where(r => (DateTime)r["Date"] >= Calendar1.SelectedDate.Date &&
(DateTime)r["Date"] <= Calendar1.SelectedDate.AddDays(1))
.ToArray();
view.RowFilter = rows.ToString();
答案 0 :(得分:1)
以下是使用RowFilter
的方法:
view.RowFilter = String.Format(
"Date >= #{0}# AND Date < #{1}#",
Calendar1.SelectedDate.ToString("MM/dd/yyyy"),
Calendar1.SelectedDate.AddDays(1).ToString("MM/dd/yyyy"));
或使用LINQ
var table = (from r in socialEvents.AsEnumerable()
where r.Field<DateTime>("Date") >= Calendar1.SelectedDate.Date
r.Field<DateTime>("Date") <= Calendar1.SelectedDate.AddDays(1)
select r).CopyToDataTable();
if (table.Rows.Count > 0)
{
DataGrid1.Visible = true;
DataGrid1.DataSource = table;
DataGrid1.DataBind();
}
else
{
DataGrid1.Visible = false;
}