LINQ尝试获取数据库记录错误本地序列不能在LINQ to SQL

时间:2018-03-12 03:24:14

标签: linq

我正在尝试通过他们的3位数字asx代码来获取公司,我正在传递空间分离的asx代码,并且希望所有拥有这些代码的公司都返回。

List<CompanyProfile> companies = null;
        try
        {
            using (var context = GetDataContext())
            {
                companies = context.CompanyProfiles.Where(x => spacesSeperatedCodes.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Any(y => string.Compare(x.ASXCode, y, StringComparison.OrdinalIgnoreCase) == 0 && x.dtRetired == null)).ToList();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error in GetCompaniesByLetter", ex);
        }
        return companies;

我得到的错误是

除了Contains运算符之外,本地序列不能用于查询运算符的LINQ to SQL实现。

1 个答案:

答案 0 :(得分:0)

为什么不按照错误生成的建议。无论如何,你可以这样做:

        List<CompanyProfile> companies = null;
        try
        {
            var codes = spacesSeperatedCodes.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            using (var context = GetDataContext())
            {
                companies = context.CompanyProfiles.Where(x => codes.Contains(x.CustomerID) && x.dtRetired == null).ToList();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error in GetCompaniesByLetter", ex);
        }
        return companies;        

我希望结果真的是你一直在寻找的。如果不是,请告诉我,好的。也许我在这里错过了什么。