locked
how to convert string to decimal in my entity using linq c# RRS feed

  • Question

  • User-2144897193 posted

    Hi All,

    The given below is my entity

    [{"Name"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309397493639320220">:</gwmw><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309397491363871620">karthi</gwmw>,"Age":"32"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309397493766735939">,,</gwmw>"<gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309397496972954198">DepositAmount</gwmw>":"25000","WithDrawAmount:"10000"},

    {"Name"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309404585371442911">:</gwmw>Raj,"Age":"40"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309404582624240927">,,</gwmw>"<gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309404581286909479">DepositAmount</gwmw>":"2000","WithDrawAmount:"1000"},

    {"Name"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309411979230289692">:</gwmw><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309411974563424276">kumar</gwmw>,"Age":"25"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309411979948677300">,,</gwmw>"<gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309411971263261633">DepositAmount</gwmw>":"15000","WithDrawAmount:"5000"},

    {"Name"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309423402931164793">:</gwmw>Ragu,"Age":"32"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309423407629568058">,,</gwmw>"<gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309423400883956695">DepositAmount</gwmw>":"250.75","WithDrawAmount:"0"},

    {"Name"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309438017367086804">:</gwmw><gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309438015966683000">karthi</gwmw>,"Age":"32"<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309438014574628254">,,</gwmw>"<gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309438011643813586">DepositAmount</gwmw>":"576.75","WithDrawAmount:"200.25}]

    I need to sort the entity based on <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309458530573272722">depositamount</gwmw> either ascending or <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309458538303412771">decending</gwmw>, <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309458535678437107">for</gwmw> that <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309458533321052867">i</gwmw> need to convert <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309458531737919934">depositamount</gwmw> and <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309458538944814938">withdrawamount</gwmw> from string to decimal.

    Could anyone help <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309471802960113053">on</gwmw> this<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309471806817925076"> .</gwmw>Need to achieve in <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309471807845705959">linq</gwmw>. The above one is <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15604309479883222379405">sample</gwmw>, but <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309479889687300786">i</gwmw> have 1000 records in my entity. 

    Note: Entity which <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309594071572871134">i</gwmw>'m getting is from <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15604309594070466006884">mongodb</gwmw>.

    Thanks in advance.

    Thursday, June 13, 2019 12:56 PM

All replies

  • User753101303 posted

    Hi,

    Likely something like :

    var src = new List<Source>() { new Source { Name = "A", Value = "1000" }, new Source { Name = "B", Value = "1.234" } };
    var dst = src.Select(o => new Target() { Name = o.Name, Value = Decimal.Parse(o.Value, System.Globalization.CultureInfo.InvariantCulture) });
    

    Storing decimal values as strings was done on purpose ? IMHO the real fix would be to change that.

    Edit: or exposed as a property that would cache the conversion result to minimize the work and transition progressively to this ? For now it really sounds a design error on the db side.

    Thursday, June 13, 2019 2:52 PM
  • User665608656 posted

    Hi kaarthikeyan,

    According to your description, you could create an entity class corresponding to your database field, and then put the data into that entity. And that's the data you gave us.

    By getting to the entity's data set from database, use the LINQ statement to get each piece of data in the data set, and change the type fields you need to change.

    For more details,you could refer to the following code: 

          public class Entity
            {
                public string Name { get; set; }
                public string Age { get; set; }
                public string DepositAmount { get; set; }
                public string WithDrawAmount { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
    
                var entity =//Here is your entity data
                var newentity = from u in entity
                          select new
                          {
                              Name = u.Name,
                              Age = u.Age,
                              DepositAmount = Convert.ToDecimal(u.DepositAmount),
                              WithDrawAmount = Convert.ToDecimal(u.WithDrawAmount)
                          };
            }

    Best Regards,

    YongQing.

    Friday, June 14, 2019 6:46 AM
  • User71929859 posted

    I need to sort the entity based on depositamount either ascending or decending, for that i need to convert depositamount and withdrawamount from string to decimal.

    Something like this should work

            Entity[] entities = new Entity[3];
            entities[0] = new Entity { Age = "32", DepositAmount = "1000" };
            entities[1] = new Entity { Age = "32", DepositAmount = "3000" };
            entities[2] = new Entity { Age = "32", DepositAmount = "2000" };
    
            Entity[] orderedArrayDesc = entities.OrderByDescending(e => decimal.Parse(e.DepositAmount)).ToArray();
            Entity[] orderedArray = entities.OrderBy(e => decimal.Parse(e.DepositAmount)).ToArray();

    Wednesday, June 26, 2019 12:56 AM