Linq. Performance

S

shapper

Hello,

Is there a difference of performance in getting assetsPage between:

IQueryable<Asset> assetsPage = context.Assets.Skip(2).Take
(20).AsQueryable();

and:

IQueryable<Asset> allAssets = context.Assets;
IQueryable<Asset> assetsPage = assetsB.Skip(2).Take(20);

I am asking this because on my Repository I already have a method to
get all assets so I could reuse it my service:

assetsPage = assetRepository.GetAll().Skip(2).Take(20);

Instead of creating another method in the repository.

I am just wondering.

Thanks,
Miguel
 
P

Peter Duniho

Hello,

Is there a difference of performance in getting assetsPage between:

IQueryable<Asset> assetsPage = context.Assets.Skip(2).Take
(20).AsQueryable();

and:

IQueryable<Asset> allAssets = context.Assets;
IQueryable<Asset> assetsPage = assetsB.Skip(2).Take(20);

I don't know. Is there?
I am asking this because on my Repository I already have a method to
get all assets so I could reuse it my service:

assetsPage = assetRepository.GetAll().Skip(2).Take(20);

Instead of creating another method in the repository.

I am just wondering.

I don't see why there would be a difference between the two examples you
posted. But the only way to know for sure is to try each and measure how
long each takes. Of all the people posting to this newsgroup, you are the
one best in the position to actually do that.

Pete
 
G

Göran Andersson

shapper said:
Hello,

Is there a difference of performance in getting assetsPage between:

IQueryable<Asset> assetsPage = context.Assets.Skip(2).Take
(20).AsQueryable();

and:

IQueryable<Asset> allAssets = context.Assets;
IQueryable<Asset> assetsPage = assetsB.Skip(2).Take(20);

Well, the second one doesn't compile because you use assetsB instead of
allAssets in the second line, but if that is corrected they do exactly
the same thing.

Note however that neither is actually fetching any data, they are just
creating objects that can fetch the data.
I am asking this because on my Repository I already have a method to
get all assets so I could reuse it my service:

assetsPage = assetRepository.GetAll().Skip(2).Take(20);

That may be a completely different thing, depending on what the GetAll
method returns.

If it actually fetches the data and returns a collection, you will
create an object that can get data from that collection using LINQ to
Objects. If it returns an object that can get the data (i.e. return
something similar to context.Assets), it works just as the two examples
above.
 

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