合并从不同表投影到一个实体的两个iqueryable [英] Merging two iqueryables that are projected to one entity from different tables

查看:171
本文介绍了合并从不同表投影到一个实体的两个iqueryable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了此答案以合并两个iqueryable.但是我总是收到以下错误:

I have tried This answer, This one and this one to merge two iqueryables. But I always receive the following error:

类型"Estudio"出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中.可以在同一查询的两个地方初始化一个类型,但前提是两个地方都设置了相同的属性,并且这些属性以相同的顺序设置.

The type 'Estudio' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

我使用以下代码将两个不同但相似的实体框架实体(EXAMEN和EXPLORACION)映射到我的域实体Estudio.

I'm mapping from two different but similar Entity Framework Entities (EXAMEN and EXPLORACION) to my domain entity Estudio, with the following code.

IQueryable<Estudio> listExamen = context.Set<EXAMEN>().Project().To<Estudio>();
IQueryable<Estudio> listExploracion = context.Set<EXPLORACION>().Project().To<Estudio>();

var listCombined = listExamen.Concat(listExploracion);

是否存在通过合并两个列表来生成IQueryable(不可枚举)的问题?如果使用 AsEnumerable(),则会在内存中执行以下过滤器(订单,汇整等).因此,我需要合并列表,但仍然可以在不执行查询的情况下将过滤器应用于合并后的列表.

Is there anyway of generate a IQueryable (not enumerable) with the merging of both list? If AsEnumerable() is used, then the following filters (Order, Take, etc) are executed on memory. So I need to merge the list but still be able to apply filter to the merged list wihtout execute the queries.

//This will force the next condition is executed on memory
    var listCombined = listExamen.AsEnumerable().Concat(listExploracion);

有可能吗?

推荐答案

我会尝试在linq查询中将您的数据选择为匿名类型,执行联合并添加您的条件.

I would try to select your data into an anonymous type in your linq query, perform the union, and add your criteria.

var listExamen = context.Examen
    .Select(x => new { x.Prop1, x.Prop2, ... }); // Add properties

var listExploracion = context.Exploraction
    .Select(x => new { x.Prop1, x.Prop2, ... }); // Add identical properties

var listCombined = listExamen.Concat(listExploracion);

var whereAdded = listCombines
    .Where(x => x.Prop1 == someValue);
var result = whereAdded
    .Skip(skipCount)
    .Take(takeCount)
    .ToList();

注意:我不知道您是否可以结合使用通用表表达式(SQL的跳过/采用)与联合查询结合使用

注意:由于我不知道您的方法(ProjectTo)

因此,我认为解决方案不是强制转换为特定类型,而是强制转换为匿名类型,因为可以将其转换为SQL.

So I think the solution is not to cast to a specific type, but to an anonymous type, since that probably can be translated to SQL.

警告:没有测试

这篇关于合并从不同表投影到一个实体的两个iqueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆