根据在 linq 查询中检索到的其他几个“列”设置状态的最佳实践是什么。
var result = (from q in query
select new Item
{
ApprovedDate = q.ApprovedDate,
CreatedDate = q.CreatedDate,
DeclinedDate = q.DeclinedDate,
Status = 0
});
我想将状态设置为 0、1、2。
(ApprovedDate == null and DeclinedDate == null) --> 0
(ApprovedDate != null and DeclinedDate == null) --> 1
(DeclinedDate != null) --> 3
所以也许是这样的:
var result = (from q in query
select new Item
{
ApprovedDate = q.ApprovedDate,
CreatedDate = q.CreatedDate,
DeclinedDate = q.DeclinedDate,
Status = (q.CreatedDate == null && q.DeclinedDate == null) ? 0 : (q.ApprovedDate != null && q.DeclinedDate == null) ? 1 : 2
});
我可能会添加更多的状态组合,所以我应该尝试在 linq 选择查询中执行此操作,在我的存储库对象中。或者稍后在控制器中执行 .ToList() 然后 foreach 列表来设置正确的状态码?
甚至超过 3 个状态码,linq 查询变得“难以”阅读。