【问题标题】:How to write Reusable linq Queries如何编写可重用的 linq 查询
【发布时间】:2014-04-28 13:54:26
【问题描述】:

这里我需要重用 linq 查询,在 if 和 else 条件等两个地方稍作改动。如何编写可重用的 linq 查询

if(some condition){
   comms = (from s in config.PromoRegistration.Communications.Cast<CommunicationGroupConfiguration>()
            from c in s.Communications.Cast<CommunicationConfiguration>()
            where s.CurrentBrand == true
            select c).ToList().FirstOrDefault();
}
else{
    comms = (from s in config.Subscriptions.Cast<CommunicationGroupConfiguration>()
             from c in s.Communications.Cast<CommunicationConfiguration>()
             where s.CurrentBrand == true
             select c).ToList().FirstOrDefault();
}

这里

config.PromoRegistration.Communications.Cast<CommunicationGroupConfiguration>() 

仅这一部分在这两个查询中发生了变化。如何有效地编写此查询。任何建议。

【问题讨论】:

    标签: c# .net linq linq-to-entities


    【解决方案1】:

    有一个正确类型的占位符:

    IQueryable<CommunicationGroupConfiguration> temp = null;
    
    if(some condition)
    {
        temp = config.PromoRegistration.Communications.Cast<CommunicationGroupConfiguration>();
    }
    else
    {
        temp = config.Subscriptions.Cast<CommunicationGroupConfiguration>();
    }
    
    comms = 
        (from s in temp
         from c in s.Communications.Cast<CommunicationConfiguration>()
         where s.CurrentBrand == true
         select c).ToList().FirstOrDefault();
    

    或者您可以使用三元运算符(我认为这更简洁):

    comms = 
        (from s in (<some condition> ? config.PromoRegistration.Communications : config.Subscriptions).Cast<CommunicationGroupConfiguration>()
         from c in s.Communications.Cast<CommunicationConfiguration>()
         where s.CurrentBrand == true
         select c).ToList().FirstOrDefault();
    

    【讨论】:

      【解决方案2】:
      // Or return IQueryable<CommunicationConfiguration> if you're using EF 
      // or a provider that supports it
      IEnumerable<CommunicationConfiguration> GetCommunicationConfiguration() 
      {
          return someCondition 
            ? config.PromoRegistration.Communications.Cast<CommunicationGroupConfiguration>().SelectMany(x => x.Communications).Cast<CommunicationConfiguration>()
            : config.Subscriptions.Cast<CommunicationGroupConfiguration>().SelectMany(x => x.CommunicationConfiguration).Cast<CommunicationConfiguration>();
      }
      
      public CommunicationConfiguration GetCurrentBrandCommunicationConfiguration() 
      {
          return GetCommunicationConfiguration()
                    .Where(x => x.CurrentBrand)
                    .FirstOrDefault();
      }
      

      【讨论】:

      • 使用 IEnumerable 将使 linq 调用 Enumerable 扩展方法而不是 Queryable 方法。
      • 是的。我还错误地在返回类型上遗漏了泛型。他没有具体说明他使用的是 EF 还是其他支持 IQueryable 的提供程序。我选择 IEnumerable 是因为 IQueryable : IEnumerable。我添加了一条评论以清除它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2021-01-15
      • 2020-12-02
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多