如何格式化linq表达式中的字符串?

给出以下代码

IQueryable customers = from Customers in db.Customers where Customers.CstCompanyName.Contains(prefixText) && Customers.CstInactive == false select Customers.CstCompanyName + " (Phone: " + Convert.ToInt64(Customers.CstPhone).ToString("###-###-#### ####") + ")"; 

这是对我的entity framework的调用。 我从数据库中返回一个电话号码。 我试图在给定的格式字符串中格式化它。 不幸的是,当我运行它时,我收到以下错误:

 LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression. 

所以我的问题是如何将此数据库对象作为格式化字符串返回?

谢谢!

我会在数据库中执行查询 ,但在本地格式化

 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("###-###-#### ####") + ")"); 

我不确定这是不是正确的方法,但我想用一些我很舒服的东西。 如果您正在使用entity framework并加载它,那么您可以将String.Format放在LINQ查询中。

 Dim ds as Entity = New Entity() ds.FinancialInstitutionTransactions.OrderByDescending(Function(c) c.TransID).Load() Dim openLoad = From tr In ds.FinancialInstitutionTransactions.Local Select TransDate = String.Format("{0:d}", tr.Date), tr.Number, tr.ToFrom, Debit = If(tr.Debit = 0, " ".ToString(), String.Format("{0:N}", tr.Debit).ToString()), Credit = If(tr.Credit = 0, " ".ToString(), String.Format("{0:N}", tr.Credit).ToString()) 

您需要引用System.Data.Entity来加载,但随后可以轻松地以任何方式格式化结果。

我希望这会对某人有所帮助。 我知道这是在vb.net中,但转换为c#并不难。