0

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..

4
  • What is the value of Percentage? Are you trying to do this in SQL? SQL doesn't have a ToString method. Commented Oct 5, 2012 at 9:43
  • you can convert it to ToList() or ToArray() to get the values. Commented Oct 5, 2012 at 9:47
  • 1
    What is card.Percentage? Commented Oct 5, 2012 at 9:52
  • Percentage is string, card.Percentage is double and I am doing this using Linq to SQL. Commented Oct 5, 2012 at 9:53

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

+1 this is a better solution. As Alex says, change `ShowDataOnClient.Percentage' to a double
0

Try this:

double Percentage = card.Percentage;

string PercentageStr = Percentage.ToString();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.