none
How do do this simple T-SQL as LINQ to SQL, please? RRS feed

  • Question

  • Hi folks,

       how can this SQL statement be done using LINQ to SQL please?

     

    select PrimaryCityId, SubdivisionId, CountryId

    from Locations

    group by PrimaryCityId, SubdivisionId, CountryId

    order by PrimaryCityId, SubdivisionId, CountryId

     

    thanks kindly Smile

    Saturday, May 10, 2008 6:02 AM

Answers

  • Hi there,

    try this.

    Pete

    Code Snippet

    from l in Locations

    group l by new { l.PrimaryCityId, l.SubdivisionId, l.CountryId } into g
    orderby g.Key.PrimaryCityId, g.Key.SubdivisionId, g.Key.CountryId
    select new { g.Key.PrimaryCityId, g.Key.SubdivisionId, g.Key.CountryId }


    Saturday, May 10, 2008 7:52 AM

All replies

  • Hi there,

    try this.

    Pete

    Code Snippet

    from l in Locations

    group l by new { l.PrimaryCityId, l.SubdivisionId, l.CountryId } into g
    orderby g.Key.PrimaryCityId, g.Key.SubdivisionId, g.Key.CountryId
    select new { g.Key.PrimaryCityId, g.Key.SubdivisionId, g.Key.CountryId }


    Saturday, May 10, 2008 7:52 AM
  • Dude - perfect.

     

    I had no idea that you can create a new .. er.. collection?? .. of group by's ... that's so kewl.

     

    i cry thinking how long i've been programming for, and LINQ has not existed.

     

    This stuff is the best. totally blowing my mind away these last few months as i linq everything i do. What i've really done with your solution is the group and order by on a list of items which was previously retrieved from a db (using linq to sql) .. then some stuff done to it .. and instead of me hitting the db again .. i'm quering against the previous collection .. reusing what i've got.

     

    supa sweet man. thanks so much!

    Saturday, May 10, 2008 12:31 PM