我想實現這樣的空值: 如果有那麼一個匹配的ID根據它過濾後的結果,否則繞過條件如何在多處理很多關係
.Where(x => x.NeighbourhoodId == (id ?? x.NeighbourhoodId)
,但我不得到了許多正確的語法一對多的關係:
public JsonResult GetPost(int? id, int? tagid)
{
var ret = from data in db.Posts.Include(x => x.Tags)
.Include(x => x.Neighbourhood)
.OrderByDescending(x => x.PostedDate)
.Where(x => x.NeighbourhoodId == (id ?? x.NeighbourhoodId)
&& x.Tags.Any(t => t.TagId == tagid))
.ToList()
select new
{
TagName = string.Join(",", data.Tags.Select(t => t.TagName)),
Message = data.Message,
// and other related stuff
}
這裏,就像你看到的,這個where子句中包含多個條件,我想篩選post.There將是唯一一個值參數。意味着如果id參數有值,那麼tagid將爲null,如果tagid爲null,那麼id會有一些值。
現在,我想如果tagid中有空值,那麼這個查詢仍然應該運行。現在,它沒有在數據庫中工作becoz,沒有空的tagid或null的帖子,如何做到這一點。有什麼建議麼??
過濾請重新表述的問題。 'id'爲'null'時會發生什麼?當'tagId'爲'null'?繞過相應的條件? –
@IvanStoev我已經編輯了這個問題在末端PLZ看看 – duke