本文介绍了从IQueryable到IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于所有f#摇杆来说,这应该是一个快速的选项,但此刻让我陷于困境。
this should be a quick one for all you f# rockers, but it's got me stuck at the moment.
我有一个F#类型,试图实现ac#接口
I've got an F# type which I'm trying to get to implement a c# interface
public interface ICrudService<T> where T: DelEntity, new()
{
IEnumerable<T> GetAll();
}
这是在c#中的实现方式:
here's how it's implemnted in c#:
public IEnumerable<T> GetAll()
{
return repo.GetAll();
}
repo.GetAll
返回 IQueryable
,但是c#编译器知道足以转换为 IEnumerable
,因为 IQueryable< T> ; :IEnumerable< T>
。但是在F#中,编译器无法运行它,而我尝试了几次尝试将其正确转换
repo.GetAll
returns an IQueryable
, but the c# compiler knows enough to convert to IEnumerable
because IQueryable<T> : IEnumerable<T>
. but In F# the compiler can't work it and I've tried a few attempt to cast it correctly
type CrudService<'T when 'T :(new : unit -> 'T) and 'T :> DelEntity>(repo : IRepo<'T>) =
interface ICrudService<'T> with
member this.GetAll () =
repo.GetAll<'T>
这给我一个类型不匹配的错误
this is giving me a type mismatch error
推荐答案
您需要转换为 IEnumerable<'T>
:
type CrudService<'T when 'T :(new : unit -> 'T) and 'T :> DelEntity>(repo : IRepo<'T>) =
interface ICrudService<'T> with
member this.GetAll () =
repo.GetAll() :> IEnumerable<'T>
这篇关于从IQueryable到IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!