0

Have been reading questions & answers, articles, they all say:

By using IEnumerable<T>, it fetches all data from the database to your application's memory and then applies filters on top of it (this is unacceptable for my project); whereas IQueryable<T> is the saviour: it does the filtering in the database and then returns only the filtered data into memory.

In my ASP.NET Core with Kestrel project, I can see how the lambda expression or LINQ is translated into SQL commands (from the Kestrel console) and I have tried in my code to use both of them, but the translated SQL query looks the same:

enter image description here

Can anyone enlighten me how to see the differences please?

Update

Here is the code:

public IQueryable<Apartment> GetApartmentsByPostcode(string postcode, int pageIndex, int pageSize)
{
    return _dataContext.Apartment
                .Include(a => a.ApartmentDetails)
                .Where(a => string.Compare(a.PostCode, postcode, true) == 0 && a.Enabled == true)
                ?.Skip((pageIndex - 1) * countPerPage)
                ?.Take(30);
}

I switched between IQueryable and IEnumerable (.AsEnumerable() at the end of the Take(30))

I know Take(), Skip, Where etc. will return IQueryable, but there is no way to make them to return IEnumerable from the root.

So may I conclude it does not matter to use IQueryable or IEnumerable as return type?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Franva
  • 6,565
  • 23
  • 79
  • 144
  • Edit your question and provide the code you used. – Michael Liu Apr 28 '19 at 02:53
  • hi @MichaelLiu please see my update, thanks – Franva Apr 28 '19 at 03:03
  • Try putting .AsEnumerable() between .Include(…) and .Where(…). – Michael Liu Apr 28 '19 at 03:06
  • hi @MichaelLiu I see your point. Normally, nobody will do that, it will be always that we do AsEnumerable() after Where or Take, OrderBy clause. So I guess it's safe to use IEnumerable as the return type as the Lambda expression will return IQueryable. – Franva Apr 28 '19 at 03:13
  • 2
    still trying to completely understand what you are trying to achieve. However, doesn't seem like `AsEnumerable` will do you any good. The operative word being **As**, it is only meant to encapsulate your output to IEnumerable and will have no logical difference – Neville Nazerane Apr 28 '19 at 03:14
  • Difference between what exactly? "By using IEnumberable, it brings all data from DB to your application memory ..." have you tried this part yourself (if yes, can you share the code?) or have you just read it from the articles? – Neville Nazerane Apr 28 '19 at 03:22
  • It would be awesome if you could provide a [mcve], including how `GetApartmentsByPostcode` is called. – mjwills Apr 28 '19 at 04:37
  • Possible duplicate of [Returning IEnumerable vs. IQueryable](https://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet) – OfirD Apr 28 '19 at 20:18
  • 1
    I wouldn't entirely call it a duplicate of that question. Based on the first para, I see the confusion in understanding when the queries execute too. – Neville Nazerane Apr 29 '19 at 02:20

0 Answers0