4
from p in context.ParentCompanies
where p.Name.Contains(e.Filter)
orderby p.Name
select new { Company = p.Name + "," + p.Country  };

当我添加p.Country或任何其他属性时,上述linq查询失败。

例外:

Could not translate expression 'Table(ParentCompany).Where(p => p.Name.Contains(Invoke(value(System.Func'1[System.String])))).OrderBy(p => p.Name)' into SQL and could not treat it as a local expression.

4

2 回答 2

4

LINQ to SQL 无法将您的字符串操作转换为 SQL。从数据库中选择 p.Name 和 p.Country,然后在单独的步骤中连接,如下所示:

var parentCompanies = (from p in context.ParentCompanies
                       where p.Name.Contains(e.Filter)
                       orderby p.Name
                       select new { p.Name, p.Country }).ToList();

var concatenated = (from c in parentCompanies
                    select new { Company = c.Name + "," + c.Country });
于 2013-01-11T23:59:37.897 回答
0

尝试创建一个class并继承ParentCompanies

public ParentCompaniesEntity : ParentCompanies
{
   public string CompanyCountry {get; set;}
}

这就是如何使用。

var parentCompanies = (from p in context.ParentCompanies
                       where p.Name.Contains(e.Filter)
                       orderby p.Name
                       select new ParentCompaniesEntity() 
                       { 
                         CompanyCountry = p.Name + ", " + p.Country
                       }).ToList();
于 2013-01-12T00:17:19.390 回答