我想编写一个动态 linq 查询,它根据两个时间框架 startTime 和 errorTime 检查“where”条件。现在的代码在这里,
IQueryable<LogFormat> errorLines = null;
if (!string.IsNullOrEmpty(errorStartTime) && !string.IsNullOrEmpty(errorTime))
{
var sTime = DateTime.Parse(errorStartTime);
var eTime = DateTime.Parse(errorTime);
errorLines = from errorLine in File
where
(errorLine.DateTime >= sTime && errorLine.DateTime <= eTime) &&
(
errorLine.Level == "ERR"
|| errorLine.Level == "WARN"
)
select errorLine;
}
else if (!string.IsNullOrEmpty(errorStartTime))
{
var sTime = DateTime.Parse(errorStartTime);
errorLines = from errorLine in File
where
errorLine.DateTime >= sTime
&&
(
errorLine.Level == "ERR"
|| errorLine.Level == "WARN"
)
select errorLine;
}
else if (!string.IsNullOrEmpty(errorTime))
{
var eTime = DateTime.Parse(errorTime);
errorLines = from errorLine in File
where
errorLine.DateTime <= eTime
&&
(
errorLine.Level == "ERR"
|| errorLine.Level == "WARN"
)
select errorLine;
}
else
{
errorLines = from errorLine in File
where
errorLine.Level == "ERR"
|| errorLine.Level == "WARN"
select errorLine;
}
我可以知道如何只进行一个查询,以便我可以动态发送开始时间和错误时间以获取结果吗?