1

以下 TSQL 查询的等效 EF 方法语法是什么?

select istb.service_id, ss.service_desc, selected=1
from istb_services istb
inner join setup_services ss on istb.service_id=ss.service_id
where istb.istb_id=3
union
select ss.service_id, ss.service_desc, selected=0
from setup_services ss
where ss.service_id not in (select service_id from istb_services where istb_id=3)

我尝试转换查询的非部分,如下所示:

var _existing = context.istb_services.Where(e => e.istb_id == IstbID);
var _others = context.setup_services.Except(_existing);

但它正在生成编译时错误:

'System.Data.Objects.ObjectQuery.Except(System.Data.Objects.ObjectQuery)' 的最佳重载方法匹配有一些无效参数

我知道我不能将不同类型的 ObjectQuery 传递给 .Except 方法,但是替代代码是什么?

谢谢,

4

1 回答 1

1

尝试以下操作:

var resultA = 
    from istb in istb_services 
    join ss in setup_services on istb.service_id equals ss.service_id
    where istb.istb_id == 3
    select new { istb.service_id, ss.service_desc, selected = true };

var resultB =            
    from ss in setup_services 
    where !istb_services.Any(istb => 
        istb.service_id == ss.service_id &&
        istb.istb_id == 3)
    select new { ss.service_id, ss.service_desc, selected = false };

var result = resultA.Union(resultB);

具有相同字段的匿名类型初始值设定项应编译为相同的匿名类型,使两个序列对Union操作兼容。

于 2012-05-28T20:00:53.520 回答