-1

I want to add a dummy member to an IQueryable and came up with this solution:

IQueryable<Geography> geographies = _unitOfWork.GeographyRepository.GetAll(); //DbSet<Geography>

var dummyGeographies = new Geography[] { new Geography { Id = -1, Name = "All" } }.AsQueryable();

var combinedGeographies = geographies.Union(dummyGeographies);

var test = combinedGeographies.ToList(); //throws runtime exc

But it throws the following exception:

Processing of the LINQ expression 'DbSet .Union(EnumerableQuery { Geography, })' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core.

How could I make it work?!

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120

2 Answers2

1
  1. you can only union on data structure which are the same
  2. IQueryable is only applicable if the query expression not been been expressed (ToList) before its run against db and you want the expression modifiable . aka nothing which which is not going to db as a query needs to be IQueryable (simple explanation better to research and understand this yourself)
List<Geography> geographies = _unitOfWork.GeographyRepository
                        .GetAll()  //DbSet<Geography>
                        .Select(o => new Geography { Id = o.Id, Name = o.Name })
                        .ToList();

List<Geography> dummyGeographies = new List<Geography>() { 
                                        new Geography[] { new Geography { Id = -1, Name = "All" } }
                                    };

var combinedGeographies = geographies.Union(dummyGeographies);

var test = combinedGeographies.ToList(); 
Seabizkit
  • 2,417
  • 2
  • 15
  • 32
0

I was able to achieve it with the following code:

IQueryable<Geography> geographies = _unitOfWork.GeographyRepository.GetAll().Select(o => new Geography { Id = o.Id, Name = o.Name });

IQueryable<Geography> dummyGeographies = _unitOfWork.GeographyRepository.GetAll().Select(o => new Geography { Id = -1, Name = "All" });

var combinedGeographies = geographies.Union(dummyGeographies);
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120