3

The following code produces the error

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

Update

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(),

what is wrong with the ternary operator?

1
  • 1
    As the ternary operator is just an if-else, it's beyond the reaches of linq. So nothing is wrong with the ternary operator. It feels quite well today but IMHO prefers not to be used :-) Commented Feb 26, 2017 at 10:44

4 Answers 4

4

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

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.

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

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
              })
}
Sign up to request clarification or add additional context in comments.

1 Comment

With all the respect, you simply ROCK. Thank you
0

I think you simply can't put an if-else inside a linq query, at least not in that spot. See this post for detailed explanation and discussion.

Oh, and specially look at the "hack" by user AyCabron, I think that could resolve your situation neatly (depending on what you exactly want and why you choose to have null pointers).

3 Comments

It seems i have a huge issue with the ternary operator
Not sure if I'm getting you right. You personally xor only your nickname :-)
Me (personally), I try to avoid it as it hides more than it clears up and the line numbers in errormessages get amibguous.
0

The problem is not with Linq in general but with Linq to objects.

since you are using IQueryable you expect the query to run in the DB, in that context, you cannot use many operators including the ternary operator.

if you tried the same code using Linq to objects (i.e Enumerable) it will succeed.

see example here: working example

Comments

0

Error The nested query is not supported. Operation1='Case' Operation2='Collect' is generated by EF when you use null within a ? statement.

EF can not convert statements like condition ? object/list : null.

In your specific example, remove .ToList() as it will also produce error when there is no rows return. EF will automatically give you null when there is no items to select.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.