使用.Contains的Linq To SQL方法失败

时间:2011-08-15 15:26:40

标签: c# linq-to-sql

我有以下Linq To SQL方法。当我单步执行代码spcCodeIDs包含我期望的七个条目时。但是我得到了

的运行时例外

Method 'Boolean Contains(System.String)' has no supported translation to SQL.

我错过了什么?

public static DataTable GetSPCCodeList()
{         

    using (var context = ProviderDataContext.Create())
    {
        IQueryable<tblProviderAdminSPCCode> tSPCCode = context.GetTable<tblProviderAdminSPCCode>();
        IList<string> spcCodeIDs = BLLCmo.FCApprovedSPCsForGreenSheet();
        return (tSPCCode
                .Where(spcCode => spcCode.Inactive == null && spcCodeIDs.Contains(spcCode.SPCCodeID)) 
                .OrderBy(spcCode => spcCode.SPCCodeID)
                .Select(spcCode => new { spcCode.SPCCodeID, spcCode.SPCDescription, spcCode.SPCCategoryID }))
                .CopyLinqToDataTable();
    }
}

2 个答案:

答案 0 :(得分:6)

LINQ to SQL只能支持包含翻译的具体列表而不是IList接口..尝试更改您的行

IList<string> spcCodeIDs = BLLCmo.FCApprovedSPCsForGreenSheet();

List<string> spcCodeIDs = BLLCmo.FCApprovedSPCsForGreenSheet().ToList();

答案 1 :(得分:0)

您需要将字符串作为参数传递给Contains。所以尝试传递spcCode.SPCCodeID.ToString()