LINQ嵌套数组和三元运算符.不支持嵌套查询. Operation1 ='Case'Operation2 ='Collect' [英] LINQ nested array and the ternary operator. The nested query is not supported. Operation1='Case' Operation2='Collect'

查看:99
本文介绍了LINQ嵌套数组和三元运算符.不支持嵌套查询. Operation1 ='Case'Operation2 ='Collect'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码会产生错误

The nested query is not supported. Operation1='Case' Operation2='Collect'

问题是我在做什么呢?我该如何解决?

The question is what am i doing so terribly wrong? How can i fix that?

IQueryable<Map.League> v = from ul in userLeagues
    select new Map.League
    {
        id = ul.LeagueID,
        seasons = 
            inc.Seasons ? (from ss in ul.Standings
                 where ss.LeagueID == ul.LeagueID
                 select new Map.Season
                 {
                      seasonId = ss.Season.SeasonId,
                      seasonName = ss.Season.SeasonName
                 }).ToList() : null,
    };

更新

我无法理解的是为什么它如此具有魅力

what i cannot follow is why this is working as a charm

seasons =  (from ss in ul.Standings
             where ss.LeagueID == ul.LeagueID
             select new Map.Season
             {
                 seasonId = ss.Season.SeasonId,
                 seasonName = ss.Season.SeasonName
             }).Distinct(),

三元运算符怎么了?

推荐答案

该异常表示您正在使用Entity Framework.在问题中总是提到LINQ实现总是很高兴的.

The exception indicates that you're using Entity Framework. Always good to mention the LINQ implementation in questions.

当LINQ针对SQL后端运行时,SQL提供程序尝试将整个语句转换为一个SQL语句.这大大减少了支持的操作类型,因为SQL比LINQ受更多的限制.请注意,变量inc.Seasons也应该是SQL语句的一部分.现在的问题是,根据一个变量本身,SQL不能返回两个不同的结果集:总是有一个固定的SELECT子句.

When LINQ runs against a SQL backend, the SQL provider tries to translate the whole statement into one SQL statement. This greatly reduces the types of operations that are supported, because SQL is far more restricted than LINQ. Note that the variable inc.Seasons should also part of the SQL statement. Now the problem is that a SQL can't return two different result set depending on a variable that's part of itself: there is always one fixed SELECT clause.

因此在表达式中有一个Case方法,该方法不受支持(我想因此也不支持后续的Collect).

So there is a Case method in the expression in a place where it's not supported (and I guess that hence the subsequent Collect isn't supported either).

您可以通过使where子句的包含部分解决此问题:

You could solve this by making the inclusion part of the where clause:

from ul in userLeagues
select new Map.League
{
    id = ul.LeagueID,
    seasons = from ss in ul.Standings
              where inc.Seasons                    // here
                 && ss.LeagueID == ul.LeagueID
              select new Map.Season
              {
                   seasonId = ss.Season.SeasonId,
                   seasonName = ss.Season.SeasonName
              })
}

这篇关于LINQ嵌套数组和三元运算符.不支持嵌套查询. Operation1 ='Case'Operation2 ='Collect'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆