C# 不支持嵌套查询

C# 不支持嵌套查询,c#,sql,linq,C#,Sql,Linq,我有一个类似于下面的查询(我的实际查询有三个类似的部分,然后Concats将它们放在一起,并应用一些额外的过滤器和排序) var articles=来自Repository.Query()中的p let article=p.article 让办公室=p.tariff类别办公室 其中p.IsDeleted==false 选择新的 { OfficeId=office.Id, Office=Office.Name, 类别=p.TariffCategory.Description, ArticleId=

我有一个类似于下面的查询(我的实际查询有三个类似的部分,然后
Concat
s将它们放在一起,并应用一些额外的过滤器和排序)

var articles=来自Repository.Query()中的p
let article=p.article
让办公室=p.tariff类别办公室
其中p.IsDeleted==false
选择新的
{
OfficeId=office.Id,
Office=Office.Name,
类别=p.TariffCategory.Description,
ArticleId=article.Id,
文章=文章.标题,
Destinations=p.ProductDestinations.Select(d=>new{Id=d.DestinationId,Name=d.Destination.Description}),
GlobalDestinations=p.AllDestinationsInOffice,
第1条最新修订日期:,
第1.2条,
article.CreatedByEmployee
};
除了分配给
目的地的任务外,一切似乎都是对的。该行产生以下错误

不支持嵌套查询。操作1='UnionAll'操作2='MultiStreamNest'


如果我去掉那条线,一切正常。有没有办法执行这样的查询?

我想了一下,与其像我建议的那样进行连接,不如从
ProductDestination
开始查询。我们感兴趣的是每个产品+目标组合的一行,就像您通过常规SQL查询看到的一样。一旦我们得到了,我们就可以对结果进行分组,这样我们就更接近于您的表示

var data = Repository.Query<ProductDestination>()
    .Where(pd => !pd.Product.IsDeleted)
    .Select(pd => 
    new {   
       Product = pd.Product,
       Destination = pd.Destination,
    })
    .GroupBy(pd => pd.Product)
    //I'm not in a position to test if EF will successfully run the below, so .ToList()
    //May not be required. However, the bulk of the work is done in the database, anyway.
    //.ToList()
    .Select(g => new {
        OfficeId = g.Key.TariffCategory.Office.Id,
        Office = g.Key.TariffCategory.Office.Name,
        Category = g.Key.TariffCategory.Description,
        ArticleId = g.Key.Article.Id,
        Article = g.Key.Article.Title,
        Destinations = g.Select(gg => new { Id = gg.Destination.DestinationId, Name = gg.Destination.Description }),
        GlobalDestinations = g.Key.AllDestinationsInOffice,
        g.Key.Article.LastReviewedDate,
        g.Key.Article.CreatedDate,
        g.Key.Article.CreatedByEmployee
    });
var data=Repository.Query()
.Where(pd=>!pd.Product.IsDeleted)
.选择(pd=>
新{
产品=局部放电产品,
目的地=pd.目的地,
})
.GroupBy(pd=>pd.Product)
//我无法测试EF是否能成功运行下面的程序,因此.ToList()
//可能不需要。然而,无论如何,大部分工作都是在数据库中完成的。
//托利斯先生()
.选择(g=>new{
OfficeId=g.Key.TariffCategory.Office.Id,
Office=g.Key.TariffCategory.Office.Name,
类别=g.Key.TariffCategory.Description,
ArticleId=g.Key.Article.Id,
Article=g.Key.Article.Title,
Destinations=g.Select(gg=>new{Id=gg.Destination.DestinationId,Name=gg.Destination.Description}),
全局目标=g.Key.AllDestinationsInOffice,
g、 Key.Article.lastReviewEdate,
g、 Key.Article.CreatedDate,
g、 Key.Article.CreatedByEmployee
});

我非常肯定,如果没有
ToList()
,上述功能应该可以正常工作,但我不敢说100%可以正常工作。然而,如前所述,大部分工作是在数据库中完成的,即使是在内存中完成,最终投影也不应该太密集。但是,如果需要
ToList()
,我们需要修改
GroupBy
,以返回通过
键选择的所有字段,否则,我们将在延迟加载和N+1查询方面遇到问题。

I看起来其他两个查询中的一个查询的输出与给出错误的行中的一个对象同名。@jdweng:哪个输出相同?目标是否定义了代码中的其他位置?也许是一个类名。可能代码中的其他地方使用了描述。@jdweng:这将转换为SQL查询。Destinations是一个相关的数据库表。我看不到任何项在何处使用相同的名称。错误消息显示“嵌套”,只有在引用在其他地方声明的对象时才会发生。这似乎有效,谢谢。我使用的
GroupBy
不足以在不查找一些参考资料的情况下这样做。(是的,
ToList()
是必需的。)
var data = Repository.Query<ProductDestination>()
    .Where(pd => !pd.Product.IsDeleted)
    .Select(pd => 
    new {   
       Product = pd.Product,
       Destination = pd.Destination,
    })
    .GroupBy(pd => pd.Product)
    //I'm not in a position to test if EF will successfully run the below, so .ToList()
    //May not be required. However, the bulk of the work is done in the database, anyway.
    //.ToList()
    .Select(g => new {
        OfficeId = g.Key.TariffCategory.Office.Id,
        Office = g.Key.TariffCategory.Office.Name,
        Category = g.Key.TariffCategory.Description,
        ArticleId = g.Key.Article.Id,
        Article = g.Key.Article.Title,
        Destinations = g.Select(gg => new { Id = gg.Destination.DestinationId, Name = gg.Destination.Description }),
        GlobalDestinations = g.Key.AllDestinationsInOffice,
        g.Key.Article.LastReviewedDate,
        g.Key.Article.CreatedDate,
        g.Key.Article.CreatedByEmployee
    });