简体   繁体   中英

linq joining, grouping, with parent roll-up

say I've got a DataTable in this format:

id | key1 | key2 | data1 | data2 | parentID
10 |  AA  | one  |  10.3 |   0.3 |  -1
10 |  AA  | two  |  20.1 |  16.2 |  -1
10 |  BB  | one  |  -5.9 |  30.1 |  -1
20 |  AA  | one  | 403.1 | -20.4 |  10
30 |  AA  | one  | 121.5 | 210.3 |  -1

and a second DataTable like so:

id | data
10 |  5500
20 | -3000
30 |   500

what I want to do is aggregate the data at the "id" level, with the second table's "data" field added to the first's net "data1", and "data2" just summed up by itself. I figured out how to do this, but what I'm stuck at is this: I want data for anything with "parentID" != -1 to be added to it's parent. so the output of the above data should be

id | data1   | data2
10 | 2927.6  |  26.2
30 |  621.5  | 210.3

is there an efficient way to do this?

edit: code sample

        DataTable dt1 = new DataTable();
        dt1.Columns.Add("id", typeof(int));
        dt1.Columns.Add("key1", typeof(string));
        dt1.Columns.Add("key2", typeof(string));
        dt1.Columns.Add("data1", typeof(double));
        dt1.Columns.Add("data2", typeof(double));
        dt1.Columns.Add("parentID", typeof(int));

        DataTable dt2 = new DataTable();
        dt2.Columns.Add("id", typeof(int));
        dt2.Columns.Add("data", typeof(double));

        dt1.Rows.Add(new object[] { 10, "AA", "one", 10.3, 0.3, -1 });
        dt1.Rows.Add(new object[] { 10, "AA", "two", 20.1, 16.2, -1 });
        dt1.Rows.Add(new object[] { 10, "BB", "one", -5.9, 30.1, -1 });
        dt1.Rows.Add(new object[] { 20, "AA", "one", 403.1, -20.4, 10 });
        dt1.Rows.Add(new object[] { 30, "AA", "one", 121.5, 210.3, -1 });

        dt2.Rows.Add(new object[] { 10, 5500 });
        dt2.Rows.Add(new object[] { 20, -3000 });
        dt2.Rows.Add(new object[] { 30, 500 });

        var groups = dt1.AsEnumerable()
                        .GroupBy(e => e["id"])
                        .Select(e => new
                                     {
                                         id = e.Key,
                                         net_data1 = e.Sum(w => (double)w["data1"]),
                                         net_data2 = e.Sum(w => (double)w["data2"])
                                     })
                        .GroupJoin(dt2.AsEnumerable(), e1 => e1.id, e2 => e2["id"],
                                   (a1, a2) => new
                                               {
                                                   id = a1.id,
                                                   net_data1 = a1.net_data1 + a2.Sum(w => (double)w["data"]),
                                                   net_data2 = a1.net_data2
                                               });

Unfortunately, SQL (and, by extension, LINQ) is not well-suited to recursion. Can the parentID column go multiple levels deep? Like this:

ID     Parent
------------------
10     -1
20     10
30     10
40     20

If you want to retrace the steps up from ID 40 to ID 10, then you should abandon a SQL/LINQ approach and just do it in code.

It sounds like a good use of a group join. Something like this might work (though it's completely untested):

var items = from parent in context.dataTable
            join child in context.dataTable on parent.id equals child.parentID into children
            where parent.parentID == -1
            select new { id = parent.id, 
                         data1 = (parent.data1 + children.Sum(c => c.data1)), 
                         data2 = (parent.data2 + children.Sum(c => c.data2)) };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM