简体   繁体   English

Linq2Sql IQueryable内另一个IQueryable

[英]Linq2Sql IQueryable inside another IQueryable

I'm trying to structure my code and be able to better maintain some of my LINQ queries. 我正在尝试构建代码并能够更好地维护一些LINQ查询。 Actually I created a new helper class with some functions but I'm having some problems executing some of my IQueryable functions inside another IQueryable functions. 实际上,我使用一些函数创建了一个新的帮助器类,但是在另一个IQueryable函数中执行某些IQueryable函数时遇到了一些问题。

When I execute the function SupplierMappings.GetSupplierAssociatedRT(int supplierID) I'm getting the following error: 当我执行功能SupplierMappings.GetSupplierAssociatedRT(int supplierID) ,出现以下错误:

Method 'System.Linq.IQueryable`1[VMPortal.DataAccessLayer.CostCentre] 方法'System.Linq.IQueryable`1 [VMPortal.DataAccessLayer.CostCentre]
GetSupplierAssociatedCCPerRT(Int32, Int32)' has no supported translation to SQL. GetSupplierAssociatedCCPerRT(Int32,Int32)'不支持对SQL的转换。

What I'm doing wrong? 我做错了什么? Below is the part of related code: 以下是相关代码的一部分:

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));
    }
}

It is pity but in linq to sql you cannot use you defined methods that returns anything except expressions. 遗憾的是,但是在linq to sql中,您不能使用定义的方法来返回除表达式之外的任何内容。 And there is limited number of expressions that can be translated to sql query (this translations is defined in linqtosql library). 可以转换为sql查询的表达式数量有限(此转换在linqtosql库中定义)。 So you cannot do what you try to do. 因此,您无法做自己想做的事情。 You could try to inline your functions. 您可以尝试内联函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM