locked
Query for ADO.NET Entiy framework RRS feed

  • Question

  • I have table

    ID           city            branch

    1            ABC           aaa

    2            PQR           ppp

    3            ABC            bbb

    4            PQR           ppp

    5            ABC            ccc

     

    Expected result table will be

    ID           city            branch

    1            ABC           aaa, bbb, ccc

    2            PQR           ppp, qqq

    How can we do it effectively.


    http://pradipbobhate.com;
    • Changed type Allen_MSDN Thursday, January 19, 2012 8:42 AM It is a question
    Wednesday, January 18, 2012 7:13 PM

Answers

  • If you're just trying to query and retrieve these results, the key here would be GroupBy:

     

    // If your original table is "Branches"
    var results = context.Branches.GroupBy(b => b.City).OrderBy(g => g.Key);
    
    foreach(var group in results)
    {
        Console.WriteLine("City {0} has branches {1}", group.Key, string.Join(",", group.Select(i => i.branch)));
    }
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Allen_MSDN Monday, January 23, 2012 2:57 AM
    Wednesday, January 18, 2012 7:33 PM

All replies

  • You would sort by city and branch within city.
     
     
    You can practice with this.
     
     
    Wednesday, January 18, 2012 7:25 PM
  • If you're just trying to query and retrieve these results, the key here would be GroupBy:

     

    // If your original table is "Branches"
    var results = context.Branches.GroupBy(b => b.City).OrderBy(g => g.Key);
    
    foreach(var group in results)
    {
        Console.WriteLine("City {0} has branches {1}", group.Key, string.Join(",", group.Select(i => i.branch)));
    }
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Allen_MSDN Monday, January 23, 2012 2:57 AM
    Wednesday, January 18, 2012 7:33 PM