代碼
if(Model.CurrentStatus == 1 || Model.CurrentStatus == 2)
{
//can display those records..
}
else if((Model.CurrentStatus == 3 || Model.CurrentStatus == 4) && Model.Date != null)
{
if(Model.Date <= 30 days)
{
//can display those records..
}
}
我已經嘗試了以下代碼,但無法按預期完全完成
@Html.Partial("Filter", new IndexModel()
{
Id = Model.Id,
Collection = Model.Collection.Where((a => a.CurrentStatus == 1 || a.CurrentStatus == 2)
&& )
})
如何將上述 if 條件轉換為 cshtml 中的 linq。謝謝
uj5u.com熱心網友回復:
else-if 關系是 OR 關系。所以簡單地將兩條線結合起來。else if 內部嵌套的 if 是 AND 關系。這將進入第二組括號
Collection = Model.Collection.Where
(
(a => a.CurrentStatus == 1 || a.CurrentStatus == 2) ||
((a.CurrentStatus == 3 || a.CurrentStatus == 4) && a.Date != null && a.Date <= 30)
)
編輯:
這是另一個建議:將可讀代碼提取到自己的方法中,該方法評估條件并回傳布爾結果。這樣,您可以創建一個可以被該Where方法接受的謂詞:
private bool IsForDisplay( ModelDataType Model )
{
if(Model.CurrentStatus == 1 || Model.CurrentStatus == 2)
{
//can display those records..
return true;
}
else if((Model.CurrentStatus == 3 || Model.CurrentStatus == 4) && Model.Date != null)
{
if(Model.Date <= 30 days)
{
//can display those records..
return true;
}
}
return false;
}
現在您可以在 linq 運算式中簡單地使用它:
@Html.Partial("Filter", new IndexModel()
{
Id = Model.Id,
Collection = Model.Collection.Where(a => IsForDisplay(a))
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490411.html
標籤:C# asp.net-mvc 林克 剃刀
