【问题标题】:Trouble with LINQ Returning Results Set from Object[] Array Within ObjectLINQ 从对象内的对象 [] 数组返回结果集的问题
【发布时间】:2019-09-03 18:09:42
【问题描述】:

考虑以下代码:

var articlesDisplay = from product in db.ProductSearchData
                      select product.articles;

articlesDisplay = articlesDisplay.Where(a => a[].body.Contains(searchString));

我正在尝试加载结果集,但在 Where 子句中使用数组表示法时出现编译器错误。我该怎么办?

所需的最终结果是一个可用于 ASP.NET MVC 分页的 var articleDisplay 对象。

感谢任何/所有人的帮助!

【问题讨论】:

  • a[].body 应该是什么意思?您是 lambda 表达式的新手吗?
  • 好吧,但不是那么新。我意识到 a[].body 不是正确的符号 - 我想知道什么是....
  • 对。那么它应该是什么意思呢?有什么要求?我不能从错误的答案中分辨出正确的答案是什么。
  • 所以让我用有问题的 JSON 源写一篇文章......很快就会看到另一篇文章。
  • { "totalCount": 59, "pageIndex": 1, "pageSize": 20, "count": 20, "articles" : [ { "id": "215098235", "title": "LES JEUNES QUI S'ENROLENT DANS L'ARMÉE", "subtitle": null, "byline": "Radio-canada Première - Faites Du Bruit", " body": "NICOLAS OUELLET...enfant dans unetente, puis une autre qui fait des push-up au-dessus de... } ] }, ... }, ... }

标签: asp.net linq ienumerable paging iqueryable


【解决方案1】:

删除数组符号

var articlesDisplay = from product in db.ProductSearchData
                      select product.articles;

articlesDisplay = articlesDisplay.Where(a => a.body.Contains(searchString));

【讨论】:

    【解决方案2】:

    lambda 表达式就像一个函数声明,但不是method name(paramters){body },而是采用parameters => body 的形式。所以这个:

    a => a[].body.Contains(searchString)
    

    和这个是一样的:

    bool Method(Article article)
    {
        return article[].body.Contains(searchString);
    }
    

    这显然是无效的,因为它不会编译。您需要一个Func<T,bool>,或者一个接受单个元素并根据是否包含它返回真或假的函数。所以你可能想要这个:

    bool Method(Article article)
    {
        return article.body.Contains(searchString);
    }
    

    意思是这样的:

    a => a.body.Contains(searchString).
    

    【讨论】:

    • 你能帮我详细解释一下吗?在我的尝试中,我从上面得到的是:articlesDisplay =articlesDisplay.Where(a => GetArticle(a, searchString)>; 而且我仍然在 GetArticle().... .第一个a和第二个之间有一个类型区别,第一个是 ProductsData.Article[] 类型,第二个是 ProductsData.Article 类型。它们不强制转换。Tnx 继续为您提供帮助!
    猜你喜欢
    • 2011-07-10
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2021-07-28
    相关资源
    最近更新 更多