mitali
mitali

Reputation: 153

converting SQL double in String

I am getting a problem when fetching double value from using linq and converting it into string. My code is:-

 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>();
        }
    }

I have tried this also:-

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

and when i pass "N2" in ToString it gives exception "Method 'System.String ToString(System.String)' has no supported translation to SQL."

and the value i got after conversion is like this:- 5.200000000000000e+001

is there anybody to help me..

Upvotes: 0

Views: 748

Answers (2)

Alex
Alex

Reputation: 7838

Your ShowDataOnClient class should store the value of Percentage as a double. You can format the way doubles are presented to the user at a later stage. How you do that depends on the control you're using to show the data.

This has the added benefit that sorting functions would work as expected. If you convert your percentage to string this early, sorting by that column won't look right

% as String     % as double
1%              1%
10%             2%
11%             10%
2%              11%
23%             23%
etc.            etc.

Upvotes: 2

Hogan
Hogan

Reputation: 70523

Try this:

double Percentage = card.Percentage;

string PercentageStr = Percentage.ToString();

This will not invoke linq to sql and give you what you want.

Upvotes: 0

Related Questions