我有很多实现特定接口的类(
)我想返回一个基类型的IEnumerable(
)从一个静态方法,但我不知道如何转换它而不做中间集合。
代码非常简单,其中一个域对象的实现如下所示:
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));
}