【发布时间】:2016-02-04 13:43:36
【问题描述】:
我想在 linq 中有多个 where 子句,但其中只有一个应该执行,我正在尝试这样的事情:
public JsonResult GetPost(int? id, int? tagid, DateTime? date)
{
var ret = from data in db.Posts.Include(x => x.Tags)
.Include(x => x.Neighbourhood)
.Where((x => x.NeighbourhoodId == id) || (y => y.PostedDate == date) || third condition).ToList()
但我无法将第二个和第三个条件放在那里,因为在 y 之后放点后,我看不到任何选项。
现在,在这三个参数中,只有一个参数有值,另外两个有空值,所以它应该只检查有值的参数。
我应该这样写查询,是不是正确的方式:
if (id != null)
{
//whole query here
}
else if (tagid != null)
{
//whole query here
}
else (date != null)
{
//whole query here
}
这是执行此操作的最佳方法还是可能的其他方法。任何建议提前很多很多。
【问题讨论】:
-
@krillgar 抱歉,我错过了这个,但是关于这个话题的答案太多了,所以我有点迷路了,没有回答这个问题
标签: c# asp.net-mvc linq