Question

I have a question related to this one. I don't want to do a calculation (aggregation), but I need to get display values from an association. In my C# code, I can directly reference the value, because the foreign key constraint made Linq generate all the necessary wiring. When I specify the IQueryable as the Gridview datasource property, and reference something that is not a column of the primary entity in the result set, I get an error that the column does not exist. As a newbie to Linq, I am guessing the assignment implicitely converts the IQueryable to a list, and the associations are lost. My question is, what is a good way to do this?

I assume that I can work around this by writing a parallel query returning an anonymous type that contains all the columns that I need for the gridview. It seems that by doing that I would hold data in memory redundantly that I already have. Can I query the in-memory data structures on the fly when assigning the data source? Or is there a more direct solution?

The gridview is supposed to display the physician's medical group associations, and the name of the association is in a lookup table.

            IQueryable<Physician> ph =
                        from phys   in db.Physicians
                        //from name   in phys.PhysicianNames.DefaultIfEmpty()
                        //from lic    in phys.PhysicianLicenseNums.DefaultIfEmpty()
                        //from addr   in phys.PhysicianAddresses.DefaultIfEmpty()
                        //from npi    in phys.PhysicianNPIs.DefaultIfEmpty() 
                        //from assoc  in phys.PhysicianMedGroups.DefaultIfEmpty()
                        where phys.BQID == bqid
                        select phys;

alt text
(source: heeroz.com)

So, based on Denis' answer, I removed all the unneeded stuff from my query. I figured that I may not be asking the right question to begin with.

Anyways, the page shows a physician's data. I want to display all medical group affiliations in a grid (and let the user insert, edit, and update affiliations). I now realize that I don't need to explicitly join in these other tables - Linq does that for me. I can access the license number, which is in a separate table, by referencing it through the chain of child associations. I cannot reference the medical group name in the gridview, which brings me back to my question:

AffiliationGrid.DataSource = ph.First().PhysicianMedGroups;

This does not work, because med_group_print_name is not accessible for the GridView:

A field or property with the name 'med_group_print_name' was not found on the
selected data source.

Again, bear with me, if it is all too obvious that I don't understand Linq ... because I don't.

Was it helpful?

Solution

Your query seems strange. You should try to simply display

ph = from phys in db.Physicians
     where phys.BQID == bqid
     select phys;

in your grid. That should work. Also, why the calls to Load()? If the DataContext is not disposed when the grid is binding, you should not need it.

If you still have issues, can you please post the error message you get, that would help...

Part 2

The problem is that you have the name is effectively not in the PhysMedGroup. You need to navigate one level down to the MedGroupLookup to access the name, since it is a property of that class.

Depending on the technology you are using (it seems to be either WinForms or Web Forms), you will need to configure your data-binding to access MedGroupLookup.med_group_print_name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top