【问题标题】:c# linq where conditional ifc# linq where 条件 if
【发布时间】:2012-12-14 01:55:31
【问题描述】:

我有一些我正在尝试重构的 linq 代码,因为它不是很好:

基本上,我想知道是否有更好的方法来执行以下操作:

if (!string.IsNullOrWhiteSpace(_filter.AssignedTo)
{
var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null 
            && cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo                                                   
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };
}
else
{
var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null 
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };
}

除了 where 子句检查 AssignTicketToUser 之外,它们都是相同的。

我希望有更好的方法来避免使用 if else 语句?我有一些这样的代码块,不想大量重复代码!

【问题讨论】:

    标签: c# .net sql sql-server linq


    【解决方案1】:
    var query = from ticket in dataClassesDataContext.TicketsIssues
                where ticket.ClosedDate == null 
                && (string.IsNullOrWhiteSpace(_filter.AssignedTo) ? true : cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo)                                                   
                select new
                {
                    Priority = ticket.TicketPriority.TicketPriorityName,
                    Description = ticket.Description.Replace("\n", ", "),
                };
    

    您可以完全摆脱 if-else 语句。将 if 条件转移到第二个 where 子句,并删除 !。第二个 where 子句成为三元运算符。

    如果条件为真,即 _filter.AssignedTo 为 null,则不要通过返回 true 来测试 _filter.AssignedTo。如果它不为 null 或为空,则继续执行原始 else 块中的子句。

    【讨论】:

    • 你可以写成(condition1 || condition2),而不是condition1 ? true : condition2
    • 谢谢,这正是我要找的东西....我自己也没有想到这一点,我感到很尴尬(新宝宝和深夜工作的结合!)。谢谢
    【解决方案2】:

    一种方法可能是:

    var query = from ticket in dataClassesDataContext.TicketsIssues
                where ticket.ClosedDate == null 
                select new
                {
                    Priority = ticket.TicketPriority.TicketPriorityName,
                    Description = ticket.Description.Replace("\n", ", "),
                };
    
    if (!string.IsNullOrWhiteSpace(_filter.AssignedTo)
        query = query.Where(w => cUser.GetUserNameUsingGUID(w.AssignTicketToUser) == _filter.AssignedTo));
    

    【讨论】:

      【解决方案3】:

      看看PredicateBuilderimplementation from C# In a NutshellAnd 方法应该可以在这里以更通用的方式解决您的问题,并有助于加深对 LINQ 和表达式树的理解。你最终会得到类似的东西:

      var query = from ticket in dataClassesDataContext.TicketsIssues
                  where ticket.ClosedDate == null
                  select new
                  {
                      Priority = ticket.TicketPriority.TicketPriorityName,
                      Description = ticket.Description.Replace("\n", ", "),
                  };
      
      if (!string.IsNullOrWhiteSpace(_filter.AssignedTo))
      {
           query = query.And(ticket => cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-22
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 1970-01-01
        相关资源
        最近更新 更多