考虑下面的代码如何格式化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