我有许多实现特定接口的类(ISearchable),我想返回IEnumerable的基类型( ISearchable),但我不知道如何进行转换而不进行中间集合。将具体类型的通用集合转换为基类型的集合
的代码非常简单,域对象的实现方式之一,是像这样:
public class account : ISearchable
{
public static IEnumerable<ISearchable> Search(string keyword)
{
// ORMVendorCollection<T> implements IQueryable<T>
ORMVendorCollection<account> results = /* linq query */
// this works if I change the return type to IEnumerable<account>
// but it uglifies client code a fair bit
return results.AsEnumerable<account>();
// this doesn't work, but it's what I'd like to achieve
return results.AsEnumerable<ISearchable>();
}
}
客户端代码,理想情况下是这样的:
public static IEnumerable<ISearchable> Search(string keyword)
{
return account.Search(keyword)
.Concat<ISearchable>(order.Search(keyword))
.Concat<ISearchable>(otherDomainClass.Search(keyword));
}
谢谢贾里德。编辑:*等待10分钟* – 2010-09-10 17:57:12