Get from IQueryable to IEnumerable

this should be fast for all of you f # rockers, but it made me stuck at the moment.

I have an F # type that I am trying to implement for a C # interface

public interface ICrudService<T> where T: DelEntity, new() { IEnumerable<T> GetAll(); } 

here, as implemented in C #:

 public IEnumerable<T> GetAll() { return repo.GetAll(); } 

repo.GetAll returns IQueryable , but the C # compiler knows enough to convert it to IEnumerable , because IQueryable<T> : IEnumerable<T> . but in F # the compiler cannot handle it, and I tried several attempts to do it right.

 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 gives me a type mismatch error

+7
casting f #
source share
2 answers

You need to specify 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> 
+7
source share

What if you used Seq.cast <'T? It seems that you just need to direct it to the correct type. Since IEnumerables are implemented differently in F # than in C #.

 let seqCast : seq<int> = Seq.cast repo.GetAll<'T> 
+1
source share

All Articles