score:1

Accepted answer
var query =
    from xe in doc.element("taxdata").elements("table")
    from gr in xe.elements("row").groupby(_ => _.attribute("locid").value)
    select new
    {
        id = xe.attribute("id").value,
        locid = gr.key,
        rows = gr.count(),
    };

score:0

var xe = doc.descendants("table")
            .elements("row")
            .groupby(a => a.attribute("locid").value)
            .select(a => new { value = a.key, count = a.count() });

score:2

string[] tableids = doc.descendants("table").select(t => t.attribute("id").value).toarray<string>();

foreach (var id in tableids)
{
    var rowgroups = doc.descendants("table").elements()
        .where(row => row.parent.attribute("id").value.equals(id))
        .groupby(row => row.attribute("locid").value)
        .select(row => row);
    // ... do something ...
    // here you will have all the grouped xelements in rowgroups
}

Related Query

More Query from same tag