I'm using Entity Framework 6.1.3 and I'm getting the following an exception in case I `Concat` two sub queries, where the queries contain an inner join with other tables. This is the exception:
> System.InvalidOperationException : The specified cast from a materialized 'System.Int32' type to the 'System.Boolean' type is not valid.
With its stack trace:
```
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader'1.GetValue(DbDataReader reader, Int32 ordinal)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator'1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper'1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator'1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable'1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable'1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable'1 source)
```
Here is my LINQ query:
``` C#
var a =
from asset in this.entities.Assets
select new AssetInboxItem
{
MappedToGard = true,
ResidualValue = new Money
{
Currency = new CurrencyInfo { Code = asset.Currency.Code }
},
};
var p =
from asset in this.entities.PreAssets
select new AssetInboxItem
{
MappedToGard = false,
ResidualValue = new Money
{
Currency = new CurrencyInfo { Code = asset.CurrencyCode }
},
};
var results = a.Concat(p).FirstOrDefault();
```
Here is the executed SQL query:
``` sql
SELECT TOP (1)
[c].[C1] AS [C1],
[c].[C2] AS [C2],
[c].[C3] AS [C3],
[c].[C4] AS [C4],
[c].[Code] AS [C5]
FROM (SELECT
1 AS [C1],
1 AS [C2],
1 AS [C3],
cast(1 as bit) AS [C4],
[Extent2].[Code] AS [Code]
FROM [dbo].[Asset] AS [Extent1]
INNER JOIN [dbo].[Currency] AS [Extent2] ON [Extent1].[CurrencyId] = [Extent2].[Id]
UNION ALL
SELECT
1 AS [C1],
1 AS [C2],
1 AS [C3],
cast(0 as bit) AS [C4],
[Extent3].[CurrencyCode] AS [CurrencyCode]
FROM [dbo].[PreAsset] AS [Extent3]) AS [c]
```
When I change the first LINQ query to the following:
``` c#
var a =
from asset in this.entities.Assets
select new AssetInboxItem
{
MappedToGard = true,
ResidualValue = new Money
{
Currency = new CurrencyInfo { Code = "EUR" }
},
};
```
I get a `NullReferenceException` originating from the `CTreeGenerator.VisitSetOp` method:
```
at System.Data.Entity.Core.Query.PlanCompiler.CTreeGenerator.VisitSetOp(SetOp op, Node n, AliasGenerator alias, Func`3 setOpExpressionBuilder)
at System.Data.Entity.Core.Query.PlanCompiler.CTreeGenerator.Visit(UnionAllOp op, Node n)
at System.Data.Entity.Core.Query.InternalTrees.UnionAllOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n)
at System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n)
at System.Data.Entity.Core.Query.PlanCompiler.CTreeGenerator.Visit(ConstrainedSortOp op, Node n)
at System.Data.Entity.Core.Query.InternalTrees.ConstrainedSortOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n)
at System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n)
at System.Data.Entity.Core.Query.PlanCompiler.CTreeGenerator.VisitAsRelOp(Node inputNode)
at System.Data.Entity.Core.Query.PlanCompiler.CTreeGenerator.BuildProjection(Node relOpNode, IEnumerable`1 projectionVars)
```
If I remove the assignments to the CurrencyCode.Code property, the query is executed successfully. Here's how the working code looks like:
``` C#
var a =
from asset in this.entities.Assets
select new AssetInboxItem
{
MappedToGard = true,
ResidualValue = new Money
{
Currency = new CurrencyInfo { }
},
};
var p =
from asset in this.entities.PreAssets
select new AssetInboxItem
{
MappedToGard = false,
ResidualValue = new Money
{
Currency = new CurrencyInfo { }
},
};
var results = a.Concat(p).FirstOrDefault();
```
And this is the executed SQL query:
``` sql
SELECT TOP (1)
[c].[C1] AS [C1],
[c].[C2] AS [C2],
[c].[C3] AS [C3],
[c].[C4] AS [C4],
[c].[C5] AS [C5]
FROM (SELECT
1 AS [C1],
cast(1 as bit) AS [C2],
1 AS [C3],
1 AS [C4],
cast(1 as bit) AS [C5]
FROM [dbo].[Asset] AS [Extent1]
UNION ALL
SELECT
1 AS [C1],
cast(0 as bit) AS [C2],
1 AS [C3],
1 AS [C4],
cast(1 as bit) AS [C5]
FROM [dbo].[PreAsset] AS [Extent2]) AS [c]
```
I think I spotted 2 bugs here. I hope you can fix this in a patch release.
Comments: **EF Team Triage:** We are transitioning this project to GitHub (https://github.com/aspnet/EntityFramework6). As part of this transition we are bulk closing a large number of issues in order that our new issue tracker will accurately reflect the work that our team is planning to complete on the EF6.x code base.
Moving forwards, our team will be fixing bugs, implementing small improvements, and accepting community contributions to the EF6.x code base. Larger feature work and innovation will happen in the EF Core code base (https://github.com/aspnet/EntityFramework). **Closing a feature request in the EF6.x project does not exclude us implementing the feature in EF Core. In fact, a number of popular feature requests for EF have already been implemented in EF Core (alternate keys, batching in SaveChanges, etc.).**
This is a bulk message to indicate that this issue was closed and not ported to the new issue tracker. The reasons for not porting this particular issue to GitHub may include:
* It was a bug report that does not contain sufficient information for us to be able to reproduce it
* It was a question, but sufficient time has passed that it's not clear that taking the time to answer it would provide value to the person who asked it
* It is a feature request that we are realistically not going to implement on the EF6.x code base
Although this issue was not ported, you may still re-open it in the new issue tracker for our team to reconsider (https://github.com/aspnet/EntityFramework6/issues). **We will no longer be monitoring this issue tracker for comments, so please do not reply here.**