Vincenzo
Vincenzo

Reputation: 1811

Linq query performance between C# and LINQPad

In my Application is use this linq query :

var r = from uev in UtilisateurEpisodeVus
        group uev by uev.Episode.Saison.Serie into pgroup
        let count = pgroup.Count()
        orderby count descending
        select new SerieVu() { nombreDeVus = count, Serie = pgroup.Key };
return r.ToList();

In LINQPad is use approximately the same query using the same connection with Entity Framework:

from uev in UtilisateurEpisodeVus
group uev by uev.Episode.Saison.Serie into pgroup
let count = pgroup.Count()
orderby count descending
select pgroup.Key

In my application the:

resultat.ToList()

take about 45 seconds.

In LINQPad it take 191 milliseconds.

What is the problem please?

Upvotes: 0

Views: 371

Answers (2)

Amy B
Amy B

Reputation: 110201

When you say

IEnumerable<UtilisateurEpisodeVu>   listUtilisateurEpisodeVu
from uev in listUtilisateurEpisodeVu
group uev

You are grouping an IEnumerable<T>, which turns into a call on System.Linq.Enumerable.GroupBy, which loads the whole table and executes locally.


When you say

from uev in Query(uev => uev.userepivu_date > dateIlYaSeptJours)
group uev

You are grouping an IQueryable<T>, which turns into a call on System.Linq.Queryable.GroupBy, which is translated into sql and sent to the database.

You can get the same result by changing the type of the variable:

IQueryable<UtilisateurEpisodeVu>   listUtilisateurEpisodeVu
from uev in listUtilisateurEpisodeVu
group uev

Upvotes: 2

Vincenzo
Vincenzo

Reputation: 1811

Thank you for your help I have found the solution using Entity Framework Profiler trial.. In fact I was doing this :

IEnumerable<UtilisateurEpisodeVu>   listUtilisateurEpisodeVu = Query(uev => uev.userepivu_date > dateIlYaSeptJours);

var resultat =
from uev in listUtilisateurEpisodeVu
group uev by uev.Episode.Saison.Serie into pgroup
let count = pgroup.Count()
orderby count descending
select new SerieVu() { nombreDeVus = count, Serie = pgroup.Key };

And by doing :

var resultat =
from uev in Query(uev => uev.userepivu_date > dateIlYaSeptJours)
group uev by uev.Episode.Saison.Serie into pgroup
let count = pgroup.Count()
orderby count descending
select new SerieVu() { nombreDeVus = count, Serie = pgroup.Key };

using Query directly in my linq query the performance is great... I don't understand why. If someone have any idea, i will be glad to know the answer. Thanks again

Upvotes: 3

Related Questions