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.;
    Wednesday, January 18, 2012 7:13 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. -
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, January 18, 2012 7:33 PM

All replies