0

我正在尝试构建我的代码并能够更好地维护我的一些 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));
    }
}
4

1 回答 1

0

很遗憾,但在 linq to sql 中,您不能使用定义的方法返回除表达式之外的任何内容。并且可以翻译为 sql 查询的表达式数量有限(这种翻译在 linqtosql 库中定义)。所以你不能做你想做的事。您可以尝试内联您的功能。

于 2013-05-11T18:34:38.090 回答