0

linq を使用して double 値を取得し、それを文字列に変換するときに問題が発生します。私のコードは次のとおりです:-

 public List<ShowDataOnClient> GetCardListToShow()
    {
        try
        {
            List<ShowDataOnClient> CardList = new List<ShowDataOnClient>();
            using (ProgressCardLINQDataContext c = new ProgressCardLINQDataContext())
            {
                CardList = (from card in c.GetTable<T_PROGRESSCARD>()
                            where card.RecordStatus.Equals(RecordStatus.Active)
                            select new ShowDataOnClient
                            {
                                Student=(from student in c.T_STUDENTs
                                         where student.Id.Equals(card.StudentId)
                                         select student.Name).Single().ToString(),
                                Test=(from test in c.T_TESTs
                                      where test.Id.Equals(card.TestId)
                                      select test.Name).Single().ToString(),
                                MaxMarks = (from test in c.T_TESTs
                                            where test.Id.Equals(card.TestId)
                                            select test.MaxMarks).Single().ToString(),
                                MarksObtain=card.MarksObtain.ToString(),
                                Percentage=card.Percentage.ToString()

                            }).ToList<ShowDataOnClient>();
            }
            return CardList;
        }
        catch
        {
            return new List<ShowDataOnClient>();
        }
    }

私もこれを試しました: -

 Percentage=Math.Truncate(card.Percentage).ToString()

ToStringに「N2」を渡すと、「メソッド 'System.String ToString(System.String)' has no supported translation to SQL.」という例外が発生します。

変換後に得た値は次のようになります:- 5.200000000000000e+001

私を助けてくれる人はいますか..

4

2 に答える 2

2

クラスは の値をとしてShowDataOnClient保存する必要があります。後の段階で、double をユーザーに提示する方法をフォーマットできます。その方法は、データを表示するために使用しているコントロールによって異なります。Percentagedouble

これには、ソート機能が期待どおりに機能するという追加の利点があります。パーセンテージを早い段階で文字列に変換すると、その列による並べ替えが正しく表示されません

% as String     % as double
1%              1%
10%             2%
11%             10%
2%              11%
23%             23%
etc.            etc.
于 2012-10-05T10:09:00.667 に答える
0

これを試して:

double Percentage = card.Percentage;

string PercentageStr = Percentage.ToString();

これは linq to sql を呼び出さず、必要なものを提供します。

于 2012-10-05T09:59:36.503 に答える