2012-12-26 110 views
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

+0

從查詢中刪除「where」子句時會出現什麼異常? – Dima

+0

發生同樣的異常。 – neo

回答

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