Failed to translate expression

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

The above linq fails when I add p.Country or any other attribute.

An exception:

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
source share
2 answers

LINQ to SQL is not able to translate your string manipulation to SQL. Select both p.Name and p.Country from your database and then connect in a separate step, for example:

 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 }); 
+4
source

Try creating a class and inherit ParentCompanies

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

and how to use it.

 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(); 
0
source

All Articles