如何格式化一个LINQ表达式的字符串? [英] How to format a string in a linq expression?
本文介绍了如何格式化一个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("###-###-#### ####") + ")";
这是我的实体框架的呼叫。我返回从数据库中的电话号码。我想它在给定的格式字符串格式。不幸的是,当我运行它,我收到以下错误:
This is a call to my entity framework. I am returning a phone number from the database. I am trying to format it in the given format string. Unfortunately, when I run this, I receive the following error:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
所以我的问题是我怎么回到这个数据库对象作为一个格式化字符串?
So my question is how do I return this database object as a formatted string?
谢谢!
推荐答案
我想执行的查询的中数据库,但是的格式的本地:
I would perform the querying in the database, but the formatting locally:
var customers = db.Customers
.Where(c => c.CstCompanyName.Contains(prefixText))
.Select(c => new { c.CstCompanyName, c.CstPhone })
.AsEnumerable() // Switch to in-process
.Select(c => c.CstCompanyName +
" (Phone: " +
Convert.ToInt64(Customers.CstPhone)
.ToString("###-###-#### ####") + ")");
这篇关于如何格式化一个LINQ表达式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文