我的项目在嵌套Query中使用了子查询。虽然在内部查询中使用子查询,但外部查询中声明的变量对其内部的查询变得不可用。 调试嵌套查询时发现错误: 1.“字段列表”中的未知列“ Join3.appcategoryid” 2.“ where子句”中的未知列“ Extent1.taxonomyid”
enter code here :
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;
}
}