我是LINQ的新手,我需要在同一个LINQ查询上传递不同的条件,如:
var rslt = (from t in cr.faultStat
where(parameter)
select new{
name=t.Name,
Fname=t.fname
});
Run Code Online (Sandbox Code Playgroud)
在where中看到的参数,是我想发送条件动态的部分,但我不知道如何,因为在哪里接受布尔值,但我想发送类似的东西:
string parameter="date>"2018-10-10" && id="123"
Run Code Online (Sandbox Code Playgroud)
一般我应该如何动态地向linq发送参数
我猜(根据你的评论)实际上你没有字符串,但你有多个条件.然后我建议像这样编写你的查询:
var query = cr.faultStat;
// the user selected a min-date-filter?
if(minDate.HasValue)
{
query = query.Where(x => x.Date > minDate.Value);
}
// the user selected a max-date-filter?
if(maxInclusiveDate.HasValue)
{
query = query.Where(x => x.Date <= maxInclusiveDate.Value);
}
// the user selected an id-filter?
if(id.HasValue)
{
query = query.Where(x => x.Id == id.Value);
}
var rslt = query
.Select(x => select new
{
t.name,
t.fname, ...
});
Run Code Online (Sandbox Code Playgroud)
实际上在此之后,由于延迟执行上述方法,数据库(或任何来源)甚至没有被查询过一次.您必须使用foreach或执行此操作的方法:
int resultCount = rslt.Count(); // like this
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |