How we extend the percentage function to calculate the percentage of its items.
Example:
Id
Name
Phone
Address
1001
AAAAAAAAA
1234567890
XXXXXXXXXXXX
BBBBBBBBBB
1234567891
1003
CCCCCCCCCC
1234567892
1004
1234567893
VVVVVVVVVVVVVV
Find the percentage about each property in the container.
Total: 4
Id: 75%
Name = 75%
Phone: 100%
Address: 50% => (address / (decimal)total) * 100,
I used to find the number of it available in list and calculate the percentage once by one, there is any other way to calculate the percentage by using generic extension methods.
I used to find the number of it available in list and calculate the percentage once by one, there is any other way to calculate the percentage by using generic extension methods.
You could use linq where clause so that you don't need to loop through the data.
Below is my code.
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("id", typeof(Int32)));
table.Columns.Add(new DataColumn("Name", typeof(string)));
table.Columns.Add(new DataColumn("Phone", typeof(string)));
table.Columns.Add(new DataColumn("Address", typeof(string)));
table.Rows.Add(1001, "AAA", "1234", "XXX");
table.Rows.Add(DBNull.Value, "BBB", "1234", DBNull.Value);
table.Rows.Add(1003, "CCC", "1234", DBNull.Value);
table.Rows.Add(1004, DBNull.Value, "1234", "XXX");
//chanage datatable to enumerable so that linq could be used and use where to filter the data //r["id"] != DBNull.Value is a boolean , and linq will return the record when it is true
double percentId = (double)table.AsEnumerable().Where(r => r["id"] != DBNull.Value).Count() / table.Rows.Count;
double percentName = (double)table.AsEnumerable().Where(r => r["Name"] !=DBNull.Value).Count() / table.Rows.Count;
double percentPhone = (double)table.AsEnumerable().Where(r => r["Phone"] != DBNull.Value).Count() / table.Rows.Count;
double percentAddress = (double)table.AsEnumerable().Where(r => r["Address"] != DBNull.Value).Count() / table.Rows.Count;
Response.Write("total:" + table.Rows.Count + "<br/>");
Response.Write("id:" + string.Format("{0:0%}", percentId )+"<br/>");
Response.Write("name:" + string.Format("{0:0%}", percentName ) + "<br/>");
Response.Write("phone:" + string.Format("{0:0%}", percentPhone ) + "<br/>");
Response.Write("address:" + string.Format("{0:0%}", percentAddress ) + "<br/>");
}
The result.
Best regards,
Ackerly Xu
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.
That's good, am looking to a more generic way to handle percentage, because in my scenario that specific logic used more than one collection that's the reason am planning to implement generic/extends the percentage.
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.
Member
583 Points
261 Posts
Linq Extension Methods for aggregate
Dec 10, 2018 10:51 AM|jayakumarvinayagam|LINK
Hai All,
How we extend the percentage function to calculate the percentage of its items.
Example:
Find the percentage about each property in the container.
Total: 4
Id: 75%
Name = 75%
Phone: 100%
Address: 50% => (address / (decimal)total) * 100,
I used to find the number of it available in list and calculate the percentage once by one, there is any other way to calculate the percentage by using generic extension methods.
Thanks,
All-Star
43761 Points
18734 Posts
Re: Linq Extension Methods for aggregate
Dec 10, 2018 01:35 PM|mgebhard|LINK
The Enumerable type has a average method.
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.average?view=netframework-4.7.2
All you need to do is pass the column collection to the Average method.
Contributor
3460 Points
1300 Posts
Re: Linq Extension Methods for aggregate
Dec 11, 2018 06:11 AM|Ackerly Xu|LINK
Hi jayakumarvinayagam,
You could use linq where clause so that you don't need to loop through the data.
Below is my code.
The result.
Best regards,
Ackerly Xu
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.
Member
583 Points
261 Posts
Re: Linq Extension Methods for aggregate
Dec 11, 2018 07:47 AM|jayakumarvinayagam|LINK
That's good, am looking to a more generic way to handle percentage, because in my scenario that specific logic used more than one collection that's the reason am planning to implement generic/extends the percentage.
Contributor
3460 Points
1300 Posts
Re: Linq Extension Methods for aggregate
Dec 12, 2018 01:38 AM|Ackerly Xu|LINK
Hi Jayakumar Vinayagam,
You could encapsulate a method of your own, for example.
Best regards,
Ackerly Xu
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.