我正在尝试构建我的代码并能够更好地维护我的一些LINQ查询。实际上我创建了一个带有一些函数的新助手类,但是我在另一个IQueryable
函数中执行了一些IQueryable
函数时遇到了一些问题。
当我执行函数SupplierMappings.GetSupplierAssociatedRT(int supplierID)
时,我收到以下错误:
方法'System.Linq.IQueryable`1 [VMPortal.DataAccessLayer.CostCentre]
GetSupplierAssociatedCCPerRT(Int32,Int32)'没有支持的SQL转换。
我做错了什么?以下是相关代码的一部分:
public class SupplierMappings
{
private static DataLayer dl = DataLayer.GetDataContext();
public static IQueryable<ResourceType> GetSupplierAvailableRT(int supplierID)
{
return dl.ResourceTypes.Where(x =>
dl.SuppliersCompanies2.Any(y => y.SupplierID == supplierID
&& y.ResourceTypeID == x.ResourceTypeID
&& y.StatusID == (short)SuppliersCompaniesStatusEnum.Active));
}
public static IQueryable<ResourceType> GetSupplierAssociatedRT(int supplierID)
{
return GetSupplierAvailableRT(supplierID).Where(x =>
// Check if we have at least one CC associated with that RT
GetSupplierAssociatedCCPerRT(supplierID, x.ResourceTypeID).Count() >= 1);
}
public static IQueryable<CostCentre> GetSupplierAvailableCCPerRT(int supplierID, int rtID)
{
return dl.CostCentres.Where(x => x.StatusID == (short)CostCentersStatusEnum.Active
// Check than the supplier is mapped at supplier level at same company & RT
&& dl.SuppliersCompanies2.Any(y => y.CompanyID == x.CompanyID
&& y.SupplierID == supplierID
&& y.ResourceTypeID == rtID
&& y.StatusID == (short)SuppliersCompaniesStatusEnum.Active)
// Check than the PA is active
&& x.DeliveryGroup.StatusID == (short)DeliveryGroupsStatusEnum.Active);
}
public static IQueryable<CostCentre> GetSupplierAssociatedCCPerRT(int supplierID, int rtID)
{
return GetSupplierAvailableCCPerRT(supplierID, rtID).Where(x =>
dl.SuppliersCostCentre2.Count(y => y.SupplierID == supplierID
&& y.StatusID == (short)SuppliersCostCentreStatusEnum.Inactive
&& y.ResourceTypeID == rtID) != dl.SuppliersCompanies2.Count(y => y.SupplierID == supplierID
&& x.CompanyID == y.CompanyID
&& y.StatusID == (short)SuppliersCompaniesStatusEnum.Active
&& y.ResourceTypeID == rtID)
&& dl.SuppliersPracticeAreas.Count(y => y.SupplierID == supplierID
&& y.StatusID == (short)SuppliersPracticeAreaStatusEnum.Inactive
&& y.ResourceTypeID == rtID) != dl.SuppliersCompanies2.Count(y => y.SupplierID == supplierID
&& x.CompanyID == y.CompanyID
&& y.StatusID == (short)SuppliersCompaniesStatusEnum.Active
&& y.ResourceTypeID == rtID));
}
}
答案 0 :(得分:0)
很可惜但是在linq to sql中你不能使用你定义的方法来返回除表达式之外的任何东西。并且可以转换为sql查询的表达式数量有限(此转换在linqtosql库中定义)。所以你不能做你想做的事。 您可以尝试内联函数。