Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My project has subqueries used inside nested Query. While using a subquery inside the inner query, variable declared in outer query becomes inaccessible to inner queries inside it.
Error found while debugging nested queries:
1. Unknown column 'Join3.appcategoryid' in 'field list'
2. Unknown column 'Extent1.taxonomyid' in 'where clause'

C#
public static dynamic GetTaxonomies(int businessUnitId, int memberId, string businessUnitType)
       {
           using (var db = new ProtocolManagementDatabaseEntities())
           {
               var taxonomies = (from taxonomy in db.taxonomies
                                 join businessUnitTaxonomy in db.businessunittaxonomies 
                                 on taxonomy.taxonomyid equals 
                                  businessUnitTaxonomy.taxonomyid
                                 let TaxonomyID = taxonomy.taxonomyid
                                 where businessUnitTaxonomy.businessunitid == 
                                 businessUnitId
                                 select new
                                 {
                                     TaxonomyId = taxonomy.taxonomyid,
                                     TaxonomyName = taxonomy.taxonomyname,
                                     preferenceValue = db.userpreferences.Where(x => 
                                     x.entitycolumnvalue == TaxonomyID &&
                                     x.memberid == memberId && 
                                     x.entitytablename == "Taxonomy" && 
                                     x.sequencenumber == 5)
                                     .Select(x => x.isvisible).FirstOrDefault()

                                     AppCategory = (from appCategory in 
                                     db.appcategories
                                     join appCategoryTaxonomy in 
                                     db.appcategorytaxonomies on 
                                     appCategory.appcategoryid equals 
                                     appCategoryTaxonomy.appcategoryid
                                     let AppCategoryID = appCategory.appcategoryid
                                     where appCategory.parentappcategoryid == null &&
                                                   
                                 appCategory.appcategorylevel.Equals("AppCategoryName")
                                                    && 
                                 appCategoryTaxonomy.taxonomyid == TaxonomyID &&
                                 appCategoryTaxonomy.businessunitid == businessUnitId
                                 orderby appCategory.name ascending
                                 select new
                                   {
                                     AppCategoryId = appCategory.appcategoryid,
                                     AppCategoryName = appCategory.name,
                                     AppCategoryDeletionEnable = 
                                     db.appcategories.Where(x => 
                                     x.parentappcategoryid == 
                                      AppCategoryID).FirstOrDefault() != null ? true : 
                                     false
                                     SubCategory = from subCategory in db.appcategories
                                     let SubCategoryID = db.appcategories.Where(x => 
                                     x.appcategoryid == 
                                     subCategory.appcategoryid).Select(x => 
                                     x.appcategoryid).FirstOrDefault()
                                     where subCategory.parentappcategoryid == 
                                     AppCategoryID && 
                                     subCategory.businessunitid == businessUnitId
                                     orderby subCategory.name ascending
                                     select new
                                         {
                                           AppCategoryId = subCategory.appcategoryid,
                                           AppCategoryName = subCategory.name,
                                           Description = subCategory.description,
                                           IsGlobal = subCategory.isglobal,
                                           IsEnable = subCategory.isenable,
                                           ParentAppCategoryID = 
                                          appCategory.parentappcategoryid,
                                                                          
                                          SubCategoryDeleteEnable = 
                                          db.protocolappcategories.Where(x => 
                                          x.appcategoryid == SubCategoryID)
                                                                          
                                          .FirstOrDefault() != null ? true : false,

                                                                          
                                            AppCategoryLevel = 
                                            subCategory.appcategorylevel,
                                            IsExpand = false,
                                                                      }
                                                    }).ToList()
                                 }).ToList();
               return taxonomies;
           }
       }


What I have tried:

Error found while debugging nested queries:
1. Unknown column 'Join3.appcategoryid' in 'field list'
2. Unknown column 'Extent1.taxonomyid' in 'where clause'
Posted
Updated 23-Nov-18 20:52pm
v8
Comments
CHill60 22-Nov-18 7:51am    
Flatten your query structure so that you don't have nested queries?
Member 14063588 22-Nov-18 9:00am    
I am not able to understand the reply you have given, please explain it
CHill60 22-Nov-18 10:14am    
Simplify your query so that you are not using sub-queries or nested queries. Capture the information you need from the first query then pass it to the next. It will make your code easier to understand ... just because you can do something doesn't mean that you should
Member 14063588 22-Nov-18 22:41pm    
Can you please help me out to simplify this query?
CHill60 23-Nov-18 3:38am    
Sorry, no - I don't have Visual Studio here and it's too complex to attempt by eye. Others may see it though now that I've edited your post.
I notice that you removed the pre-tags that I added to your post. Be aware that if your code is properly formatted (as it is now) it is easier for members to read ... and if it is easier to read more people are likely to try to help

1 solution

Nobody is going to look at this and "do your job".

It's a crappy query you need to simplify (i.e. "flatten") by creating "intermediate" queries that feed each other (using "temp" results / collections if you have to).

You can't fix something you don't understand. How do you "understand"? By simplifying.

What you have is "technical debt" that you now have to "pay for" (or continue being "confused").

Yeah ... and it's going to take a "few hours".
 
Share this answer
 
v2

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