LINQ - 当值为 NULL 时排除过滤器

问题描述 投票:0回答:5

我想看看当有一个可以为 NULL 的可选参数(CountryId)时是否有更好的方法来编写下面的查询

 public static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria, string countryId = null)
        {
            return repository
                .Queryable()
                .Where(x => (countryId != null ? x.CountryId == countryId : true) && x.Name.Contains(searchCriteria))
                .AsEnumerable();
        }

理想情况下,当 CountryId 为 NULL 时,我想排除过滤器中的条件。

-艾伦-

c# linq linq-to-sql linq-to-entities
5个回答
1
投票
public static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria, string countryId = null)
    {
        return repository
            .Queryable()
            .Where(x => (countryId == null) || (x.CountryId == countryId && x.Name.Contains(searchCriteria)).AsEnumerable();
    }

1
投票

你不能分步骤构建它吗:

public static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria, string countryId = null)
    {
         var ret = repository
          .Queryable()
            .Where(x=>x.Name.Contains(searchCriteria));

          if (!(countrId == null))
          {
             ret = ret.Where(y=>y.CountryId == countryId )
          }
         return ret.AsEnumerable();;
     }

0
投票

我在 jquery 数据表服务器端过滤上遇到了这个问题。我的解决方案是如果变量为空,则与它自己的变量进行比较。这里有你的代码:

 public static IEnumerable<Game> GameByMatchingName(this 
 IRepositoryAsync<Game> repository,  string searchCriteria, string countryId = null)
    {
        return repository
            .Queryable()
            .Where(x => ((countryId != null && x.CountryId == countryId) || (countryId == null && x.CountryId == x.CountryId)) && x.Name.Contains(searchCriteria))
            .AsEnumerable();
    }

0
投票

不要让sql查询中无缘无故出现不必要的参数

 public static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria, string countryId = null)
        {
            if(countryId == null)
            {
                return repository
                .Queryable()
                .Where(x => x.Name.Contains(searchCriteria))
                .AsEnumerable();
            }
            else {
                 return repository
                 .Queryable()
                 .Where(x => x.CountryId == countryId && x.Name.Contains(searchCriteria))
                 .AsEnumerable();
            }
            
        }

0
投票

死灵术:

一般来说,如果您更喜欢重载函数而不是默认参数,则可以避免不必要的 if/else 语句:


private static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria, Func<Game, bool> filter) => repository
    .Queryable()
    .Where(x => filter(x) && x.Name.Contains(searchCriteria))
    .AsEnumerable();

public static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria) => repository
    .GameByMatchingName(searchCriteria, g => true);

public static IEnumerable<Game> GameByMatchingName(this IRepositoryAsync<Game> repository,  string searchCriteria, string countryId) => repository
    .GameByMatchingName(searchCriteria, g => g.CountryId == countryId);

生成的代码总是更清晰,意图也更容易理解。维护开发人员将感谢您降低了圈复杂度。

© www.soinside.com 2019 - 2024. All rights reserved.