将linq结果转换为List< MyInterface> [英] Cast linq results to List<MyInterface>
本文介绍了将linq结果转换为List< MyInterface>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经扩展了实体,以实现针对其类型的特定接口.我正在尝试执行以下查询:
I have extended my entities to implement specific interfaces for its type. I am trying to perform the following query:
var results = from x in context.MyEntityTable
where x.AProperty == AValue
select x;
return results.Count() > 0 ? results.Cast<IApplicationEntity>().ToList() : null;
但是,我不断收到以下错误消息:
However, I keep getting the following error:
"LINQ to Entities仅支持转换实体数据模型原始类型"
基本上,我要做的是始终将结果从原始实体类型转换为它实现的接口的通用列表.
Basically what I want to do is always convert the results from the raw entity type to a generic list of the interface it implements.
这可能吗?
推荐答案
You can do the cast on the client, bypassing the entity framework query translation layer by calling AsEnumerable
extension method:
return results.Any()
? results.AsEnumerable().Cast<IApplicationEntity>().ToList()
: null;
但是,最好颠倒执行Count
检查的顺序:
However, it's better to reverse the order of doing the Count
check:
var list = results.AsEnumerable().Cast<IApplicationEntity>().ToList();
return list.Count == 0 ? null : list;
这篇关于将linq结果转换为List< MyInterface>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文