简体   繁体   English

linq 条件查询

[英]linq conditional query

What would be the best practice for setting a status depending on several other "columns" retrieved in a linq query.根据在 linq 查询中检索到的其他几个“列”来设置状态的最佳实践是什么。

  var result = (from q in query
                select new Item
                         {
                           ApprovedDate = q.ApprovedDate,
                           CreatedDate = q.CreatedDate,
                           DeclinedDate = q.DeclinedDate,
                           Status = 0
                         });

I'd like to set the status to either 0, 1, 2.我想将状态设置为 0、1、2。

(ApprovedDate == null and DeclinedDate == null) --> 0
(ApprovedDate != null and DeclinedDate == null) --> 1
(DeclinedDate != null) --> 3

So perhaps something like:所以也许是这样的:

  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
                         });

I might add even more status combinations, so should I try and do this in the linq select query, in my repository object.. Or later on in the controller where I would do a .ToList() and then foreach the list to set the correct status code?我可能会添加更多状态组合,所以我应该尝试在 linq 选择查询中,在我的存储库对象中执行此操作。正确的状态码?

Having even more than 3 statuscodes, the linq query gets "hard" to read.甚至超过 3 个状态码,linq 查询变得“难以”阅读。

What about moving status calculation to Item class?将状态计算移动到Item类怎么样? If status property depends on other properties value, then it's definitely calculated property:如果 status 属性依赖于其他属性值,那么它肯定是计算属性:

var result = from q in query
             select new Item
                         {
                           ApprovedDate = q.ApprovedDate,
                           CreatedDate = q.CreatedDate,
                           DeclinedDate = q.DeclinedDate
                         });

And

public class Item
{
  // other properties

  public int Status
  {
      get
      {
          if (ApprovedDate == null and DeclinedDate == null)
              return 0;
          if (ApprovedDate != null and DeclinedDate == null)
              return 1;
          if (DeclinedDate != null)
              return 3;
          // etc
      }
  }
}

Actually I think it's best option, because in this case status calculation logic will be close to required data.实际上我认为这是最好的选择,因为在这种情况下状态计算逻辑将接近所需的数据。 If (for some reason) you can't use this approach, then move setting statuses to local items collection:如果(出于某种原因)您不能使用此方法,则将设置状态移动到本地项目集合:

var items = result.ToList().ForEach(i => i.Status = CalculateStatus(i));

Maybe wrapped all in a function An do a linq like this也许将所有内容都包装在一个函数中 像这样做一个 linq

var result = (from q in query sele q).AsEnumerable()
                                       .Select( x => new Item()
                                       {
                                           ApprovedDate = x.ApprovedDate,
                                           CreatedDate = x.CreatedDate,
                                           DeclinedDate = x.DeclinedDate,
                                           Status = MyStatusFunction(x.CreatedDate,q.DeclinedDate)
                                       });


public int MyStatusFunction(DateTime ApprovedDate , Datetime DeclinedDate)
{
    if (ApprovedDate == null and DeclinedDate == null) return 0;
    else if(ApprovedDate != null and DeclinedDate == null) return 1;
    else if (DeclinedDate != null) return 3;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM