2012-03-02 18 views
6

は、次のコード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. 

だから私の質問は、私はフォーマットされた文字列として、このデータベースオブジェクトを返しますどのようにでしょうか?

ありがとうございます!

+0

結果が 'IQueryable'である必要がありますか?あなたは 'var customers = ...'を使って 'IEnumerable 'を得ることはできませんか? –

+0

しないでください。データベースにデータを処理させる。プレゼンテーションコードでプレゼンテーションを処理する。 – AakashM

+0

最後に、文字列[]として返されているので、これを実現するものは何でも。私が現在行っていることは、string [] cst = customers.ToArray();です。だから、IQueryableにする必要があるかどうかはよく分かりません。 – Kevin

答えて

21

は、私は、データベース内のを照会行うだろうが、は、ローカルのフォーマット:

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

が、私はわからないんだけど、これは適切な方法であれば、私は、私は「何かを使用するように思っています私は快適です。エンティティフレームワークを使用して読み込んでいる場合、LINQクエリにString.Formatを配置できます。

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#に変換するのは難しくありません。

関連する問題