Linq query how to build nested objects from one table

I have one table, and I need to collect a bunch of nested objects based on one table.

Data:

PointA PointB Month Time Price 1 2 11 11:00 10,99 1 2 12 11:00 9.99

The objects

POINTS {PointA, PointB, Details} Details {Month, ExtraDetails} ExtraDetails {Time, Price}

I want to avoid the loads of loops and if statements, so I should be able to use linq to do this. but that goes beyond my work experience.

edit: They need grouping, and

any help would be great.

thanks

+3
source share
2 answers

Just tried the solution:

var nestedObjects = from row in data
    select new {row.PointA, row.PointB, Details = new {
        row.Month, ExtraDetails = new {
            row.Time, row.Price
        }
    }};

This assumes that you have already received your data in the data.


Group

, "Group By":

var nestedObjects = from row in data
    group row by new { row.PointA, row.PointB } into Points
    select new {
        Points = Points.Key,
        Details = from details in Points
            select new { row.Month, ExtraDetails = new {
                row.Time, row.Price
            }}
    };

- , , , , , Points. : , , , , .

+3

, , , , , LINQ, POINTS.

- :

var res = 
    from item in table.AsEnumerable()
    select new Points(){PointA = item["PointA"];
                           PointB = item["PointB"]; 
                           Details = from item2 in table.AsEnumberable()
                                     where item["PointA"] = item2["PointA"] and item["PointB"] = item2["PointB"]
                                     select new Details(){
                                                          month=item2["month"],
                                                          extraDetails = from item3 in table.AsEnumerable()... 
                                                         }
                        };

res IEnumerable of Points

, .NET 3.5,

0
source

Source: https://habr.com/ru/post/1710134/


All Articles