gpt4 book ai didi

c# - 基于参数值的linq中的多个where

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:37 24 4
gpt4 key购买 nike

<分区>

我想在 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
}

这是执行此操作的最佳方法还是其他方法。如果有任何建议,请提前致谢。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com