locked
Unintentional rounding of decimal fields in linq to entitities RRS feed

  • Question

  • I have a "from" query using linq to entities. The query works fine but there are decimal numbers which are in sql server and have 3 distinct decimal places. Once the query is executed for reasons I don't know the decimal numbers all were rounded to two decimal places. The fields that are being rounded are:

                                                      Mina = cc.Min.ToString(),
                                                      MinThresha = cc.MinThresh.ToString(),
                                                      Max =cc.Max.ToString(),
                                                      MaxThresh=cc.MaxThresh.ToString(),

    The use of ToString was an experiment I used to see if it might prevent the rounding from occurring; it didn't help-the rounding still occurred. Basically I had original simply selected the fields (e.g. cc.Min)

    Here is the full query.

                            var headerid = Guid.Parse(hdnHeaderId.Value.ToString());
                            var measuresHeader = (from cc in dbGen.DocumentMeasureCCTrans
                                                  join head in dbGen.DocumentHeaders
                                                  on cc.DocumentHeaderId equals head.DocumentHeaderId
                                                  join m in dbGen.DocumentMeasureTypes
                                                  on new { cc.MeasureName, head.DocumentTypeId }
                                                  equals new { m.MeasureName, m.DocumentTypeId }
                                                  where head.DocumentHeaderId.Equals(headerid)
                                                  //&& m.DocumentTypeId.Equals(head.DocumentTypeId)
                                                  select new
                                                  {
                                                      TOOL_CHANGE = cc.TOOL_CHANGE,
                                                      cc.DocumentMeasureCCTransId,
                                                      cc.MeasureName,
                                                      Mina = cc.Min.ToString(),
                                                      MinThresha = cc.MinThresh.ToString(),
                                                      Max =cc.Max.ToString(),
                                                      MaxThresh=cc.MaxThresh.ToString(),
                                                      mrange = cc.MinThresh + " - " + cc.MaxThresh                                ,
                                                      GuageName = m.GuageName,
                                                      cc.Measurement,
                                                      cc.Sort,
                                                      head.DocumentHeaderId,
                                                      head.DocumentTypeId
                                                  }).ToList().OrderBy(s => s.Sort).ToList();
                            //var measureHeaderList = measuresHeader.OrderBy(s => s.Sort).ToList();
                            o = measuresHeader;

    I would appreciate any help.

    Thanks,

    Neil


    Fig000

    Wednesday, October 28, 2020 1:59 PM

All replies

  • Have you tried it?

    ToString ("0.###");

    • Marked as answer by fig000 Friday, October 30, 2020 9:33 PM
    • Unmarked as answer by fig000 Thursday, November 5, 2020 4:32 PM
    Wednesday, October 28, 2020 8:19 PM
  • Thank you. I spent time on this and saw some references to the use of tostring but could not find the specific pattern. You are the man.

    Fig000

    Friday, October 30, 2020 9:33 PM
  • Sorry to back out on the marking this as an answer. It worked in one limited extent but when I ran it in a more complete real world example I got this error:

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

    Here is the linq that is causing the issue:

                            var measuresHeader = (from cc in dbGen.DocumentMeasureCCTrans
                                                  join head in dbGen.DocumentHeaders
                                                  on cc.DocumentHeaderId equals head.DocumentHeaderId
                                                  join m in dbGen.DocumentMeasureTypes
                                                  on new { cc.MeasureName, head.DocumentTypeId }
                                                  equals new { m.MeasureName, m.DocumentTypeId }
                                                  where head.DocumentHeaderId.Equals(headerid) 
                                                  //&& m.DocumentTypeId.Equals(head.DocumentTypeId)
                                                  select new
                                                  {
                                                      TOOL_CHANGE = cc.TOOL_CHANGE,
                                                      cc.DocumentMeasureCCTransId,
                                                      cc.MeasureName,                                                
                                                      Min=cc.Min.ToString("0.###"),
                                                      MinThresh=cc.MinThresh.ToString("0.###"),
                                                      Max=cc.Max.ToString("0.###"),
                                                      MaxThresh =cc.MaxThresh.ToString("0.###"),
                                                      mrange = cc.MinThresh + " - " + cc.MaxThresh,
                                                      GuageName = m.GuageName,
                                                      cc.Measurement,
                                                      cc.Sort,
                                                      head.DocumentHeaderId,
                                                      head.DocumentTypeId
                                                  }).ToList().OrderBy(s => s.Sort).ToList();
                            //var measureHeaderList = measuresHeader.OrderBy(s => s.Sort).ToList();
                            o = measuresHeader;

    Any help would be appreciated.

    Thanks,

    Neil


    Fig000

    Thursday, November 5, 2020 4:41 PM
  • I would say stop using an anaymous type in the Linq projection  and use a custom type that has decimal properties.

    select new Customtype

    {

          MinThresh=cc.MinThresh,}

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    Thursday, November 5, 2020 11:14 PM