I am getting query data back from a stored procedure, and I need to group this into 3 generic lists. I'm not sure how to get this right.
Here is what the query result from the data looks like in summary:
Child | Parent | GrandChildren
a2 | a1 | a30
a2 | a1 | a31
a2 | a1 | a32
b2 | b1 | b30
b2 | b1 | b31
c2 | c1 | c30
d2 | d1 | d30
d2 | d1 | d31
d3 | d1 | d32
d3 | d1 | d33
Here is how I want to group the data.
public class Parent
{
public int ID { get; set; }
public string Name { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int ID { get; set; }
public string Name { get; set; }
public List<GrandChild> GrandChildren { get; set; }
}
public class GrandChild
{
// numerous properties.
}
Here is what I've tried, but with no luck.
var data = Repository.GetData();
var groupedData = from d in data
where d.Parent > 0
group d by d.Parent into ParentGroup
select parentGroup.ToList();
// Result: Grouped list of "Parent", not with grouped "Child" and grouped "GrandChildren" as per desired structure above.
You're going to have to group the grandchildren, too. Something like this:
var data = Repository.GetData();
// Group the Grandchildren first
var grandkids = from d in data
where d.Child > 0
group d by d.Child into GrandChildGroup
select new Grandchild {
// Assign grand child properties here
};
// Group the Children
var children = from d in data
where d.Parent > 0
group d by d.Parent into ChildGroup
select new Child {
// Assign child properties here
Grandchildren = ( from gc in grandkids
where gc.Parent = d.Child
select gc ).ToList()
};
// Now group the parents
var groupedData = from p in data
select new Parent {
// Assign parent properties here
Children = ( from kid in children
where kid.Parent = p.Parent
select kid ).ToList()
}.ToList();
I am getting query data back from a stored procedure, and I need to group this into 3 generic lists. I'm not sure how to get this right.
Here is what the query result from the data looks like in summary:
Child | Parent | GrandChildren
a2 | a1 | a30
a2 | a1 | a31
a2 | a1 | a32
b2 | b1 | b30
b2 | b1 | b31
c2 | c1 | c30
d2 | d1 | d30
d2 | d1 | d31
d3 | d1 | d32
d3 | d1 | d33
Here is how I want to group the data.
public class Parent
{
public int ID { get; set; }
public string Name { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int ID { get; set; }
public string Name { get; set; }
public List<GrandChild> GrandChildren { get; set; }
}
public class GrandChild
{
// numerous properties.
}
Here is what I've tried, but with no luck.
var data = Repository.GetData();
var groupedData = from d in data
where d.Parent > 0
group d by d.Parent into ParentGroup
select parentGroup.ToList();
// Result: Grouped list of "Parent", not with grouped "Child" and grouped "GrandChildren" as per desired structure above.
You're going to have to group the grandchildren, too. Something like this:
var data = Repository.GetData();
// Group the Grandchildren first
var grandkids = from d in data
where d.Child > 0
group d by d.Child into GrandChildGroup
select new Grandchild {
// Assign grand child properties here
};
// Group the Children
var children = from d in data
where d.Parent > 0
group d by d.Parent into ChildGroup
select new Child {
// Assign child properties here
Grandchildren = ( from gc in grandkids
where gc.Parent = d.Child
select gc ).ToList()
};
// Now group the parents
var groupedData = from p in data
select new Parent {
// Assign parent properties here
Children = ( from kid in children
where kid.Parent = p.Parent
select kid ).ToList()
}.ToList();
0 commentaires:
Enregistrer un commentaire