不支持嵌套查询。Operation1 ='UnionAll'Operation2 ='MultiStreamNest'

乔纳森·伍德

我有以下查询。

var query = Repository.Query<Product>()
    .Where(p => !p.IsDeleted && p.Article.ArticleSections.Count() > 0)
    .Select(p => new
    {
        OfficeId = p.TariffCategory.Office.Id,
        Office = p.TariffCategory.Office.Name,
        Category = p.TariffCategory.Description,
        ArticleId = p.Article.Id,
        Article = p.Article.Title,
        Destinations = p.ProductDestinations.OrderBy(pd => pd.Destination.Description).Select(pd => new { Id = pd.DestinationId, Name = pd.Destination.Description }),
        GlobalDestinations = p.AllDestinationsInOffice,
        p.Article.LastReviewedDate,
        p.Article.CreatedDate,
        p.Article.CreatedByEmployee
    });
query = query.Concat(Repository.Query<Package>()
    .Where(pkg => !pkg.IsDeleted && pkg.Article.ArticleSections.Count() > 0)
    .Select(pkg => new
    {
        OfficeId = pkg.TariffCategory.Office.Id,
        Office = pkg.TariffCategory.Office.Name,
        Category = pkg.TariffCategory.Description,
        ArticleId = pkg.Article.Id,
        Article = pkg.Article.Title,
        Destinations = pkg.PackageDestinations.OrderBy(pd => pd.Destination.Description).Select(pd => new { Id = pd.DestinationId, Name = pd.Destination.Description }),
        GlobalDestinations = pkg.AllDestinationsInOffice,
        pkg.Article.LastReviewedDate,
        pkg.Article.CreatedDate,
        pkg.Article.CreatedByEmployee
    }));
query = query.Concat(Repository.Query<Backgrounder>()
    .Where(bkgd => !bkgd.IsDeleted && bkgd.Article.ArticleSections.Count() > 0)
    .Select(bkgd => new
    {
        OfficeId = bkgd.TariffCategory.Office.Id,
        Office = bkgd.TariffCategory.Office.Name,
        Category = bkgd.TariffCategory.Description,
        ArticleId = bkgd.Article.Id,
        Article = bkgd.Article.Title,
        Destinations = bkgd.BackgrounderDestinations.OrderBy(bd => bd.Destination.Description).Select(bd => new { Id = bd.DestinationId, Name = bd.Destination.Description }),
        GlobalDestinations = bkgd.AllDestinationsInOffice,
        bkgd.Article.LastReviewedDate,
        bkgd.Article.CreatedDate,
        bkgd.Article.CreatedByEmployee
    }));

// Apply filters
if (OfficeIds.Any())
    query = query.Where(a => OfficeIds.Contains(a.OfficeId));
if (DestinationIds.Any())
    query = query.Where(a => a.GlobalDestinations || a.Destinations.Any(d => DestinationIds.Contains(d.Id)));
if (!string.IsNullOrEmpty(ArticleTitle))
    query = query.Where(a => a.Article.Contains(ArticleTitle));
if (!string.IsNullOrEmpty(TariffCategory))
    query = query.Where(a => a.Category.Contains(TariffCategory));

// Sort results
query = query.OrderBy(a=> a.Office).ThenBy(a => a.Category).ThenBy(a => a.Article);

var articles = query.ToList();

但是,当我运行此查询时,出现异常。

不支持嵌套查询。Operation1 ='UnionAll'Operation2 ='MultiStreamNest'

该查询搜索我数据库中的文章。由于文章可以涉及到ProductPackage或者Backgrounder,我需要从相关表中的信息,我串连为每个项目单独的查询。

我已将其范围缩小到Destinatons显然,这构成了与相关联的查询中的查询Concat()(如果我删除了后两个查询和关联的Concat()调用,它将正常工作。)

在看了一段时间之后,我无法看到另一种构造查询的方法,而又没有使其变得非常慢。

有人能看到我可能错过的任何技巧来解决该异常吗?

伊万·斯托夫(Ivan Stoev)

不幸的是,没有技巧。我认为在不完全重写的情况下完成这项工作的唯一合理方法是分别执行查询(应用所有可能的过滤器),然后Concat在内存中执行和排序,如下所示:

var queries = new [] 
{
    Repository.Query<Product>()
    .Where(p => !p.IsDeleted && p.Article.ArticleSections.Count() > 0)
    .Select(p => new
    {
        OfficeId = p.TariffCategory.Office.Id,
        Office = p.TariffCategory.Office.Name,
        Category = p.TariffCategory.Description,
        ArticleId = p.Article.Id,
        Article = p.Article.Title,
        Destinations = p.ProductDestinations.OrderBy(pd => pd.Destination.Description).Select(pd => new { Id = pd.DestinationId, Name = pd.Destination.Description }),
        GlobalDestinations = p.AllDestinationsInOffice,
        p.Article.LastReviewedDate,
        p.Article.CreatedDate,
        p.Article.CreatedByEmployee
    }),

    Repository.Query<Package>()
    .Where(pkg => !pkg.IsDeleted && pkg.Article.ArticleSections.Count() > 0)
    .Select(pkg => new
    {
        OfficeId = pkg.TariffCategory.Office.Id,
        Office = pkg.TariffCategory.Office.Name,
        Category = pkg.TariffCategory.Description,
        ArticleId = pkg.Article.Id,
        Article = pkg.Article.Title,
        Destinations = pkg.PackageDestinations.OrderBy(pd => pd.Destination.Description).Select(pd => new { Id = pd.DestinationId, Name = pd.Destination.Description }),
        GlobalDestinations = pkg.AllDestinationsInOffice,
        pkg.Article.LastReviewedDate,
        pkg.Article.CreatedDate,
        pkg.Article.CreatedByEmployee
    }),

    Repository.Query<Backgrounder>()
    .Where(bkgd => !bkgd.IsDeleted && bkgd.Article.ArticleSections.Count() > 0)
    .Select(bkgd => new
    {
        OfficeId = bkgd.TariffCategory.Office.Id,
        Office = bkgd.TariffCategory.Office.Name,
        Category = bkgd.TariffCategory.Description,
        ArticleId = bkgd.Article.Id,
        Article = bkgd.Article.Title,
        Destinations = bkgd.BackgrounderDestinations.OrderBy(bd => bd.Destination.Description).Select(bd => new { Id = bd.DestinationId, Name = bd.Destination.Description }),
        GlobalDestinations = bkgd.AllDestinationsInOffice,
        bkgd.Article.LastReviewedDate,
        bkgd.Article.CreatedDate,
        bkgd.Article.CreatedByEmployee
    }),
};

// Apply filters
if (OfficeIds.Any())
    for (int i = 0; i < queries.Length; i++) queries[i] = queries[i].Where(a => OfficeIds.Contains(a.OfficeId));
if (DestinationIds.Any())
    for (int i = 0; i < queries.Length; i++) queries[i] = queries[i].Where(a => a.GlobalDestinations || a.Destinations.Any(d => DestinationIds.Contains(d.Id)));
if (!string.IsNullOrEmpty(ArticleTitle))
    for (int i = 0; i < queries.Length; i++) queries[i] = queries[i].Where(a => a.Article.Contains(ArticleTitle));
if (!string.IsNullOrEmpty(TariffCategory))
    for (int i = 0; i < queries.Length; i++) queries[i] = queries[i].Where(a => a.Category.Contains(TariffCategory));

// Switch to LINQ to Objects and concatenate the results
var result = queries.Select(query => query.AsEnumerable()).Aggregate(Enumerable.Concat);

// Sort results
result = result.OrderBy(a=> a.Office).ThenBy(a => a.Category).ThenBy(a => a.Article);

var articles = result.ToList();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

LINQ 嵌套数组和三元运算符。不支持嵌套查询。操作1='案例' 操作2='收集'

来自分类Dev

不支持嵌套查询

来自分类Dev

unionAll导致StackOverflow

来自分类Dev

fsck错误:当前,仅支持1或2个FAT,不支持34

来自分类Dev

ValueError:使用带有LIKE查询的psycopg2不支持的格式字符“''

来自分类Dev

硬件不支持Spectre变体2

来自分类Dev

后端不支持 Django qs1.difference(qs2, qs3)

来自分类Dev

Spark是否并行执行UnionAll?

来自分类Dev

嵌套:QueryParsingException[[multi_match] 查询不支持 [fields]];

来自分类Dev

DB2 SQL0255-尝试使用JOIN用户ROW_NUMBER时不支持查询功能

来自分类Dev

为什么在双符号SHA2和SHA1上signtool.exe不支持/ as选项

来自分类Dev

Apigee似乎不支持OAuth 2规范,为什么?

来自分类Dev

Symfony2路由不支持的密钥

来自分类Dev

Struts 2 jQuery插件,图表插件不支持类别

来自分类Dev

WebApi2请求的资源不支持发布

来自分类Dev

Symfony2模板不支持“ bundle”参数

来自分类Dev

Python中的“按位不”不支持2的补码

来自分类Dev

Symfony2 Assert \ Expression注释不支持常量

来自分类Dev

(apache2_module)模块不支持的参数

来自分类Dev

ReadFromKafka引发ValueError:不支持的信号:2

来自分类Dev

Apigee似乎不支持OAuth 2规范,为什么?

来自分类Dev

Symfony2路由不支持的密钥

来自分类Dev

糟糕或不支持WebGL的playcanvas 2D后备

来自分类Dev

Struts 2 jQuery插件,图表插件不支持类别

来自分类Dev

xcf2png -- 不支持 XCF 版本 12

来自分类Dev

不支持 HLMT2PDF 列表类型

来自分类Dev

unionAll函数无法在sparkR中运行

来自分类Dev

错误:[布尔]查询不支持[术语]

来自分类Dev

作为查询不支持cosmos db

Related 相关文章

热门标签

归档