我怎麼能簡化這樣的表述:簡化if語句
var someList = new List<someType>();
if (String.IsNullOrEmpty(groupId))
{
someList = CTX.Values.Include(c => c.Customer).ToList();
}
else
{
someList = CTX.Values.Include(c => c.Customer).Where(c => c.GroupId== groupId).ToList();
}
的差異只在.Where(c => c.GroupId== groupId)
。查詢語句中是否可以包含條件String.IsNullOrEmpty(groupId)
?
我刪除了我的答覆,因爲我不認爲你應該 '簡化' 吧。你的代碼比已發佈的代碼更容易理解(包括我的代碼)。 –
作爲旁註:我會替換'var someList = new List();''List someList;',因爲該值不會被使用。 –
CodesInChaos
@MichaelViktorStarberg你的答案的主要問題不是它很難閱讀,但它是錯誤的。 – CodesInChaos