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.
Could anyone help on this .Need to achieve in linq. The above one is sample, but i have 1000 records in my entity.
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.
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.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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();
None
0 Points
29 Posts
how to convert string to decimal in my entity using linq c#
Jun 13, 2019 12:56 PM|kaarthikeyan|LINK
Hi All,
The given below is my entity
[{"Name":karthi,"Age":"32",,"DepositAmount":"25000","WithDrawAmount:"10000"},
{"Name":Raj,"Age":"40",,"DepositAmount":"2000","WithDrawAmount:"1000"},
{"Name":kumar,"Age":"25",,"DepositAmount":"15000","WithDrawAmount:"5000"},
{"Name":Ragu,"Age":"32",,"DepositAmount":"250.75","WithDrawAmount:"0"},
{"Name":karthi,"Age":"32",,"DepositAmount":"576.75","WithDrawAmount:"200.25}]
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.
Could anyone help on this .Need to achieve in linq. The above one is sample, but i have 1000 records in my entity.
Note: Entity which i'm getting is from mongodb.
Thanks in advance.
All-Star
48570 Points
18079 Posts
Re: how to convert string to decimal in my entity using linq c#
Jun 13, 2019 02:52 PM|PatriceSc|LINK
Hi,
Likely something like :
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.
Contributor
3710 Points
1043 Posts
Re: how to convert string to decimal in my entity using linq c#
Jun 14, 2019 06:46 AM|Yongqing Yu|LINK
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.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
52793 Points
9695 Posts
MVP
Re: how to convert string to decimal in my entity using linq c#
Jun 26, 2019 12:56 AM|Ruchira|LINK
Something like this should work
Please 'Mark as Answer' if this post helps you
My Tech Blog