Reputation: 10789
I curently have a set up like below
public class ProptsClass
{
public class ClassA
{
public list<ClassB> pcb { get; set; }
}
public class ClassB
{
public list<ClassC> pcc { get; set; }
}
public class ClassC
{
public string _someproperty { get; set; }
}
}
what I want to do is to databind object ClassA to a ListView with one of the columns being databound to ClassC._someproperty .
When I try to databind it as
<%#Eval("Namespace.ProptsClass.ClassA.pcb.pcc._someproperty") %>
I get the error: "DataBinding: Namespace ProptsClass.ClassA + ClassB' does not contain a property with the name '_someproperty'".
Can somebody tell me where I am doing wrong and what is the correct way of doing it. secondly is there a better way I can achieve the same in the code behind instead of ASP.net. Thanks in advance
Upvotes: 0
Views: 173
Reputation: 71573
DataBind on ListViews and GridViews only does a "shallow" search for property/field names to which to bind columns. Also, it will not "flatten" the list of lists you have in the structure. If you want to bind to a multilevel object model, you will need to either expose the child property on the parent via a "pass-through" property, or project the values you want to display into a one-dimensional collection of a "view-helper" class. I would choose the latter in your case, since you want the value of an element accessed multi-dimensionally.
Try this:
var displayResult = theProptsClass.A.pcb.SelectMany(b=>b.pcc).Select(c._someproperty);
The SelectMany() function acts to "flatten" the relationship between ClassB and ClassC, and combined with the Select() will return all _someproperty values of all Cs in all Bs in one A. You can chain another SelectMany() call if you have a collection of As from which you need to project a flat collection of Bs.
Upvotes: 4