考慮下面的代碼如何格式化linq表達式中的字符串?
IQueryable<string> customers =
from Customers in db.Customers
where Customers.CstCompanyName.Contains(prefixText) && Customers.CstInactive == false
select Customers.CstCompanyName + " (Phone: " + Convert.ToInt64(Customers.CstPhone).ToString("###-###-#### ####") + ")";
這是我的實體框架的呼叫。我正在從數據庫中返回一個電話號碼。我試圖格式化給定的格式字符串。不幸的是,當我運行它,我收到以下錯誤:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
所以我的問題是怎麼做的我回到這個數據庫對象爲格式的字符串?
謝謝!
你需要結果是'IQueryable'嗎?難道你不能通過'var customers = ...'獲得一個'IEnumerable'? –
2012-03-02 14:47:38
不要。讓數據庫處理數據;有演示文稿代碼處理。 – AakashM 2012-03-02 14:48:00
最後,它以字符串[]的形式返回,所以無論什麼都可以實現。我目前正在做的是string [] cst = customers.ToArray();所以我不太確定它是否需要IQueryable。 – Kevin 2012-03-02 15:04:58