Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq .net核心3中的IQueryable.Union/Concat_Linq_Asp.net Core_Iqueryable - Fatal编程技术网

Linq .net核心3中的IQueryable.Union/Concat

Linq .net核心3中的IQueryable.Union/Concat,linq,asp.net-core,iqueryable,Linq,Asp.net Core,Iqueryable,我想向IQueryable添加一个虚拟成员,并提出了以下解决方案: IQueryable<Geography> geographies = _unitOfWork.GeographyRepository.GetAll(); //DbSet<Geography> var dummyGeographies = new Geography[] { new Geography { Id = -1, Name = "All" } }.AsQueryable()

我想向IQueryable添加一个虚拟成员,并提出了以下解决方案:

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
IQueryable geographies=\u unitOfWork.GeographyRepository.GetAll()//数据库集
var dummyGeographies=newgeography[]{newgeography{Id=-1,Name=“All”}}.AsQueryable();
var combinedGeographies=地理位置。联合(dummyGeographies);
var test=组合地理图.ToList()//抛出运行时exc
但它抛出以下异常:

LINQ表达式“DbSet”的处理 由“NavigationExpandingExpressionVisitor”创建的.Union(EnumerableQuery{Geography,})”失败。这可能表明EF核心中存在缺陷或限制


我怎么能让它工作

我能够通过以下代码实现它:

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);
IQueryable geographies=\u unitOfWork.GeographyRepository.GetAll().Select(o=>newgeography{Id=o.Id,Name=o.Name});
IQueryable dummyGeographies=\u unitOfWork.GeographyRepository.GetAll().Select(o=>newgeography{Id=-1,Name=“All”});
var combinedGeographies=地理位置。联合(dummyGeographies);
  • 只能在相同的数据结构上进行联合
  • IQueryable仅适用于在对db运行查询表达式之前未表示查询表达式(ToList),并且您希望该表达式可修改的情况。也就是说,不作为查询进入数据库的任何内容都需要是可查询的(简单的解释可以更好地研究和理解这一点)
  • List geographies=\u unitOfWork.GeographyRepository
    .GetAll()//DbSet
    .Select(o=>newgeography{Id=o.Id,Name=o.Name})
    .ToList();
    List dummyGeographies=新列表(){
    新地理[]{new Geography{Id=-1,Name=“All”}
    };
    var combinedGeographies=地理位置。联合(dummyGeographies);
    var test=组合地理图.ToList();
    
    var combinedGeographies=geographies.ToList().Union(dummyGeographies.ToList()?无法在内存列表和数据库表之间执行联合,需要切换到客户端评估
    
    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();