【问题标题】:IQueryable where clauseIQueryable where 子句
【发布时间】:2015-04-21 21:42:36
【问题描述】:

我很难为这篇文章找到合适的标题。但我有以下几点:

IArticleRepository articleRepo = unitOfWork.ArticleRepository;
List<Article> articles = new List<Article>(
                         articleRepo.GetAll()
                         .Where(a => a.Title == searchTerm)
                         //.Where(a => a.Categories.Contains(Category.))
                         .OrderByDescending(a => a.CreatedDate));  

所以一些解释:一个article 有一个Title 和一个CreateDate,并且通过这些过滤很容易。但是article 也有与之关联的categories。所以article 有一个array 类型为Category 的属性。类型 Category 有一个名为 CategoryId 的属性,类型为 int

所以在我的代码中它被注释掉的地方,我试图 select 一个 article,它有一个与之关联的 category,谁的 CategoryId 等于..说4

但我发现很难用我的 C# 语法来表达这一点。我也是 C# 新手,所以这也无济于事。

【问题讨论】:

    标签: c# linq iqueryable


    【解决方案1】:

    您无需创建新列表,并且可以在一个 Where 子句中使用多个 where 表达式。可以试试下面的代码吗:

    List<Article> articles = articleRepo.GetAll()
                         .Where(a => a.Title == searchTerm && a.Categories.Contains(Category)).OrderByDescending(a => a.CreatedDate)).ToList();  
    

    【讨论】:

    • 除非添加 .ToList() 否则无法编译
    • 哦,是的,你是真的。
    • “你是真的”真是太开发者了! :)
    • 那是非常“例外”的 :)
    【解决方案2】:

    你不需要写两个Where 子句;只需将另一个条件添加到您的第一个 Where。第二个条件应该使用Any 函数来搜索您要查找的类别。

    IArticleRepository articleRepo = unitOfWork.ArticleRepository;
    List<Article> articles = new List<Article>(
                         articleRepo.GetAll()
                         .Where(a => a.Title == searchTerm &&
                                     a.Categories.Any(c => c.CategoryID == 4))
                         .OrderByDescending(a => a.CreatedDate));  
    

    对于多个类别,假设您的 CategoryID 位于名为 MyCatIDsListint[]List&lt;int&gt; 中。他们可以将上述查询中的类别子句更改为:

                  a.Categories.Any(c => MyCatIDsList.Contains(c.CategoryID))
    

    【讨论】:

    • 获取:运算符'&&'不能应用于'bool'和'Article'类型的操作数...?
    • @DeanGrobler 这是一个错字:-) 第二个不需要a =&gt;
    • 你们太聪明了:)
    • @dotNET 作为旁注。如果我想在多个 CategoryId 上进行搜索,而不是只是 '4' 我有一个包含一堆整数的任意大小的数组怎么办?
    【解决方案3】:

    使用 LINQ 查询时有另一种语法,它更像 SQL。上面的代码是正确的,但是你可能会发现这个版本更简洁:

    int categoryId = 4
    IArticleRepository articleRepo = unitOfWork.ArticleRepository;
    var articlesQuery = from article in articleRepo.GetAll()
                        from category in article.Categories
    
                        where category.CategoryId == categoryId
                        where article.Title == searchTerm
                        orderby article.CreatedDate descending
    
                        select article
    
    List<Article> articles = articlesQuery.ToList();
    

    或者更常见的是一步完成所有这些操作:

    int categoryId = 4
    List<Article> articles =   (   
                                 from article in articleRepo.GetAll()
                                 from category in article.Categories
    
                                 where category.CategoryId == categoryId
                                 where article.Title == searchTerm
                                 orderby article.CreatedDate descending
    
                                 select article
                               ).ToList()
    

    【讨论】:

    • articlecategory 具有一对多的关系,从 OP 中可以看出,因此此解决方案很可能不起作用。
    • 虽然它是一些真正漂亮干净的代码,但他在上面所说的 dotNET 是正确的。它不工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2021-04-17
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多