IQueryable and IEnumerable

S

shapper

Hello,

On my repositories I always return a IQueryable when getting multiple
records, for example in:
productRepository.GetMoreExpensive();

However, most the code I see use IEnumerable. For example:
1. ToPagedList extension ...
2. AutoMapper to mape between a source collection and a destination
collection ...

So I always need to change my IQueryable obtained through the
repository and convert it to IEnumerable to be able to use the
ToPagedList extension and the AutoMapper.

Should I use IEnumerable on my repositories?

I am afraid I am doing something wrong.

Thanks,
Miguel
 
H

Harlan Messinger

shapper said:
Hello,

On my repositories I always return a IQueryable when getting multiple
records, for example in:
productRepository.GetMoreExpensive();

However, most the code I see use IEnumerable. For example:
1. ToPagedList extension ...
2. AutoMapper to mape between a source collection and a destination
collection ...

So I always need to change my IQueryable obtained through the
repository and convert it to IEnumerable to be able to use the
ToPagedList extension and the AutoMapper.

IQueryable IS IEnumerable. IQueryable inherits IEnumerable's interface.
In addition, anything that implements IQueryable<T> implements
IEnumerable<T> and also IEnumerable as well. What are you encountering
that leads you to think you need to convert explicitly?
 
S

shapper

IQueryable IS IEnumerable. IQueryable inherits IEnumerable's interface.
In addition, anything that implements IQueryable<T> implements
IEnumerable<T> and also IEnumerable as well. What are you encountering
that leads you to think you need to convert explicitly?

Usually I return a IQueryable from my repositories ... I got that
adivice some time ago.
To be honest I don't remember the advantages but I think it might be
related with Linq.

Basically, I am using AutoMapper as follows:

Articles = Mapper.Map<IQueryable<Article>,
IEnumerable<ArticleItemViewModel>>(articleRepository.GetAll
()).ToPagedList(page.HasValue ? page.Value - 1 : 0, 20),

I am mapping a IQueryable<Article> to an
IEnumerable<ArticleItemViewModel> and finally converting to PagedList.

I got this working now ... Since IQueryable inherits IEnumerable.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top