我有一个表单,可以根据他们选择的内容过滤数据。
我正在尝试将 linq 查询相互附加,以便最终结果是他们在屏幕上选择的结果。
这是我的代码:
private void button_Search_Click(object sender, EventArgs e)
{
using (var model = new SuburbanPortalEntities())
{
var qry = from logs in model.Logs
select logs;
Guid corpid;
if (Guid.TryParse(textBox_CorporationGuid.Text, out corpid))
{
qry = from logs in model.Logs
where logs.CorporationId == corpid
select logs;
}
Guid tokenid;
if (Guid.TryParse(textBox_TokenId.Text, out tokenid))
{
qry = from logs in model.Logs
where logs.TokenId == tokenid
orderby logs.LogDateTime descending
select logs;
}
if (checkBox_DisplayErrors.Checked)
{
qry = from logs in model.Logs
where logs.IsException
select logs;
}
if (checkBox_DisplayWarnings.Checked)
{
qry = from logs in model.Logs
where logs.IsWarning
select logs;
}
dataGridView1.DataSource = qry;
}
}
我运气不好。最后一个查询是我的 datagridview 上显示的内容。
有人可以告诉我我做错了什么吗?
Thanks !
这里发生的事情是你正在重新定义qry
每次都来自您的源数据。qry
is an IEnumerable<T>
就像logs
,所以你应该能够像这样替换你的代码:
var qry = model.Logs;
if( // Condition )
qry = qry.Where(x => x.CorporationId == corpId);
if( // Another condition)
qry = qry.Where(x => x.IsException);
在此设置结束时,qry
将是所有选定的组合Where
条款,并且应该只生成您正在寻找的项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)