9

I use EF 4 and C#.

I need order the result of this query with two Properties belonging to two different Entities.

In my case I would like order by gt.GroupTypeId and its subset by cnt.ContentId.

PS: I'm not sure if my title is appropriate, if you think not, let me know I will change it :-)

from cnt in context.CmsContents
            from gt in cnt.CmsGroupsTypes
            join t in context.CmsTypes
            on cnt.TypeContent equals t.TypeContent
            join m in context.CmsModes
            on cnt.ModeContent equals m.ModeContent
            orderby gt.GroupTypeId // Problem here
            select new
            {
            cnt.ContentId,
            cnt.Title,
            gt.TypeGroup,
            gt.GroupTypeId,
            TypeContentDescription = t.Description,
            ModeContentDescription = m.Description,
            cnt.IsPublished
            };
James Hill
  • 60,353
  • 20
  • 145
  • 161
GibboK
  • 71,848
  • 143
  • 435
  • 658

1 Answers1

14

Simple example:

var orderedList = cnt.OrderBy(x => x.GroupTypeId).ThenBy(x => x.ContentId);
James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 1
    I also try.... orderby gt.GroupTypeId, cnt.ContentId .... and it is working. Can we say that your version and mine written here are the same? (Thanks for your time I'm learning EF :-) – GibboK Oct 04 '11 at 15:35
  • 1
    your `orderby` clause will be translated to `OrderBy` call. Also Linq queries work somewhat slower than the same stuff written via extension methods – vittore Oct 04 '11 at 15:39
  • thanks vittore for your comment, do you mean that my version (written with sugar syntax) could be slower that the same version with extension methods? Correct? – GibboK Oct 04 '11 at 15:43
  • 3
    @vittore: Do you have a reference for your statement "LINQ queries are slower than extension methods"? I've never heard of that. – Slauma Oct 04 '11 at 16:15
  • Slauma: I'll try to find comparison, but we come up with this results on several projects using EF, and trying to avoid LINQ syntax after that GibboK: Yes, translation of sugar syntax expression takes time to translate, while on easy queries it is not important, when you do grouping, sorting and several joins it became more obviously to notice – vittore Oct 05 '11 at 11:25
  • @Slauma: here is thread on this http://stackoverflow.com/questions/1182922/what-is-the-efficiency-and-performance-of-linq-and-lambda-expression-in-net – vittore Oct 05 '11 at 11:29
  • 1
    @vittore: My understanding is that this question compares LINQ/Extension methods vs. Non-LINQ/Non-Extension. One answer there is: "*For LINQ queries, with the 'new syntax', the IL (code) generated, is fundamentally no different than calling the extension methods provided by Enumerable and Queryable directly.*". That's what I was assuming as well. Also see this answer by Jon Skeet: http://stackoverflow.com/questions/671235/how-linq-works-internally/671425#671425 especially the part "Query expressions" where he says that LINQ query syntax is translated into extension methods *at compile time*. – Slauma Oct 05 '11 at 14:57
  • @vittore the translation of the syntax happens at compile time. There is no runtime difference. – Jon Hanna Aug 31 '17 at 17:20