14

I have two select statements in LINQ with a Union.

A RoleID needs to have a null value in one of the selects. I am getting the error below.

If the RoleID has a value, it works fine. Reports is an EF entity with properties.

It can be anything in this example. Example is simple for illustration purposes.

Code in LinqPad:

var list = Reports.Select(r => new
    {
        RoleID = 3
    })
    .Union(Reports.Select(r => new
    {
        RoleID = new Nullable<int>()    <= error
        //RoleID = (int?) null          <= error
        //RoleID = 4                    <= works
    }));
list.Dump();

How do I make it work with a null value and make RoleID of type int?

Error Message:

'System.Linq.IQueryable' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)' has some invalid arguments Instance argument: cannot convert from 'System.Linq.IQueryable' to 'System.Linq.ParallelQuery'

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Tony_Henrich
  • 42,411
  • 75
  • 239
  • 374

2 Answers2

26

Your first Select query returns sequence of anonymous objects where RoleID have type int. To union both sequences they should have same type of anonymous objects. So you need to change first query:

var list = Reports.Select(r => new
                {
                    RoleID = (int?)3
                })

Keep in mind, in second query you also should have nullable RoleID to match type of anonymous object:

 .Union(Reports.Select(r => new
    {
        RoleID = new Nullable<int>() 
        //RoleID = (int?)null
        //RoleID = (int?)4
    }));

BTW Why have union on two selects from same source? Looks like you over simplified your sample queries.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
8

You need RoleID to be nullable in the first anonymous class as well:

Reports.Select(r => new
               {
                   RoleID = (int?)3
               }

See also these questions:

Community
  • 1
  • 1
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • Any comment on the downvote? The OP has indicated that `RoleID = 4` works for them... – Kobi Jan 31 '15 at 09:50
  • Not my downvote, but didn't you change answer? Seems like you originally had changes to `Union` part of query – Sergey Berezovskiy Jan 31 '15 at 09:52
  • I need RoleID to have a value in one of the selects. Just like in SQL where you can assign a null to a select value and in another union select you can select an actual column in the same position. Concat doesn't make a difference. It gives the same error. – Tony_Henrich Jan 31 '15 at 09:53
  • @SergeyBerezovskiy - Not that I can recall. *Meybe* I starter with `r => new` but edited it in 5 seconds. – Kobi Jan 31 '15 at 09:54
  • @Tony_Henrich - `RoleID` will have a value in the first object, but you will have to use `.RoleID.Value`. You cannot have a list of object of the **same type**, but when a property is sometimes nullable. You can't do that with notmal classes either. As for Union and Concat - You generally want to use `Concat` - this is like `union` vs. `union all` in SQL. – Kobi Jan 31 '15 at 09:58