Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm stuck at the moment. I have a dataset with 2 related tables. It's a simple parent table with a single related child table.

For various reasons, I need to convert the dataset into a nested ObservableCollection. The following Linq code works with 1 issue: I'm missing a where clause on the nested collection so all of the children are part of the parent which is incorrect.

C#
this.ocApps = new ObservableCollection<Models.ApplicationTypesModel>(this.dsApps.tAppTypes.AsEnumerable().Select(apptype => new Models.ApplicationTypesModel
                {
                    AppType = apptype.nvarAppType,
                    Apps=new ObservableCollection<Models.ApplicationModel>(this.dsApps.tApps.AsEnumerable().Select(apps=>new Models.ApplicationModel
                    {
                        idAppId=apps.idAppId,
                        strAppName=apps.nvarAppName,
                        strEnvironment=apps.nvarEnvironment,
                        strAppType=apps.nvarAppType,
                        dtLastPrivACCertDate= apps.dtLastPrivACCertDt
                    }).where(the parent table.apptype value = the child table.apptype value))
                }));


I think I need a where on the inner select, which makes sense...but I'm not sure where or how to put the where.



Does anyone have some suggestions?

Thanks
Posted
Updated 12-Dec-12 23:38pm
v2

1 solution

This works perfectly. The syntax of the "where" is what I was missing. The following URL may be of help to someone else who needs to convert a relational dataset to a nested ObservableCollection. My intent is in the ViewModel of a MVVM pattern with this particular app.

http://stackoverflow.com/questions/6359980/proper-linq-where-clauses

The format of "}).Where(app=>app.strAppType==apptype.nvarAppType)" is what was throwing me.

XML
this.ocApps = new System.Collections.ObjectModel.ObservableCollection<Models.ApplicationTypesModel>(this.dsApps.tAppTypes.AsEnumerable().Select(apptype => new Models.ApplicationTypesModel
                {
                    AppType = apptype.nvarAppType,
                    Apps = new System.Collections.ObjectModel.ObservableCollection<Models.ApplicationModel>(this.dsApps.tApps.AsEnumerable().Select(apps => new Models.ApplicationModel
                    {
                        idAppId = apps.idAppId,
                        strAppName = apps.nvarAppName,
                        strEnvironment = apps.nvarEnvironment,
                        strAppType = apps.nvarAppType,
                        dtLastPrivACCertDate = apps.dtLastPrivACCertDt
                    }).Where(app=>app.strAppType==apptype.nvarAppType))
                }));
 
Share this answer
 

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