c# - LINQ 嵌套数组和三元运算符。不支持嵌套查询。操作 1 ='Case' 操作 2 ='Collect'

标签 c# linq

以下代码产生错误

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

问题是我做错了什么?我该如何解决?

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,
    };

更新

我无法理解的是为什么这是一种魅力

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 实现总是好的。

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

所以表达式中有一个 Case 方法不被支持(我猜因此后续的 Collect 也不被支持)。

您可以通过将包含部分作为 where 子句的一部分来解决此问题:

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
              })
}

关于c# - LINQ 嵌套数组和三元运算符。不支持嵌套查询。操作 1 ='Case' 操作 2 ='Collect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467165/

相关文章:

vb.net - Linq:按时间间隔分组

c# - 在 LINQ 查询中使用导航属性会导致关系多重性等问题

asp.net - LINQ 中的匿名类型成员声明符无效

linq - Nhibernate Linq In 子句

c# - 如何将表达式 x=>!x 重写为 x=>x!=true 并将 x=>x 重写为 x=>x==true

c# - IDisposable 有什么用?

c# - 将 UploadString 与 C# WebClient 一起使用时是否需要对值进行编码?

c# - Visual Studio 2015 表示 'cast is redundant' 。为什么?

c# - 无法绑定(bind)到数据源上的属性或列名称。参数名称 : dataMember

c# - 通过代码(C# 或 PowerShell)获取有关进程(如 Sysinternals Process Explorer)的 .NET 程序集信息