Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,i have 3 class in my dbml file,Person,Student,Teacher and i have a single table in sql with all property from 3 classes and one field[type] for discriminator
so i wanna read all data from table and show in listview with this code:
C#
 foreach (var item in dbcontext.Peapoles)
            {
                ListViewItem lsitem = new ListViewItem();
                lsitem.Tag = item.;
                lsitem.Text = item.id.ToString();
                ....
                listViewcompanies.Items.Add(lsitem);
}

in this code i can't access child class propery!!!
please help me!!!
sorry for my english
Posted

1 solution

Don't declare it as a var unless you absolutely have to - try declaring as the type that Peapoles returns. If Peapoles is returning the base class because it doesn't know they are derived instances, then cast the item to the inherited version and try that:
C#
MyClass derived = item as MyClass;
if (derived != null)
   {
   ...
   }
You can only use properties of derived classes if the variable you are accessing them via is derived, not base class.
 
Share this answer
 
Comments
akobar 3-Jul-12 9:57am    
tnx for reply,can you give me more description?
give me example with person,studend,,,
tnx
OriginalGriff 3-Jul-12 10:48am    
Sorry - I can't.
I have no idea what Peapoles does, how it does it, or what it returns. So I would have to guess - which is not a good idea, in computing! :laugh:
Have a look in the debugger, and see what type is returned. The chances are is a a generic IEnumberable of an existing class, which should help you.
akobar 3-Jul-12 11:29am    
do you understand my question??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900