Question

I have a datatable that looks like below

enter image description here My result Should be A=40% , B=60% .. ie 2/5 and 3/5 Group name can be A, B, C, etc...

How can i calculate the figures based on that datatable values??

Was it helpful?

Solution

You could use LINQ and cast it to a dictionary:

DataTable dt = new DataTable("test1");
dt.Columns.AddRange(new DataColumn[] { new DataColumn("TASKID"), new DataColumn("GROUPID"), new DataColumn("GROUPNAME") });
dt.Rows.Add(new object[] { 12, 2, "A" });
dt.Rows.Add(new object[] { 13, 3, "B" });
dt.Rows.Add(new object[] { 12, 2, "A" });
dt.Rows.Add(new object[] { 14, 3, null });
dt.Rows.Add(new object[] { 15, 3, "B" });
var query = (from DataRow row in dt.Rows
                group row by row["GROUPNAME"] into g
                select g).ToDictionary(x => (x.Key.ToString() == "" ? "*" : x.Key.ToString()), x => (int)((x.Count() * 100) / dt.Rows.Count));

Iterate through the dictionary to display the values:

foreach(KeyValuePair<string,int> kvp in query)
    Console.WriteLine(kvp.Key + " - " + kvp.Value.ToString());

The output:

A - 40
B - 40
* - 20

The percentage is cast as an int. simply change (int)((x.Count() * 100) / dt.Rows.Count) if you need more accurate values.

OTHER TIPS

A simple way, through loop. The following should be similar to the one you require.

//Simulated datatable
DataTable table1 = new DataTable();
table1.Columns.Add(new DataColumn("TaskID", typeof(int)));
table1.Columns.Add(new DataColumn("GroupID", typeof(int)));
table1.Columns.Add(new DataColumn("GroupName", typeof(String)));

//Entered test values
DataRow dr1 = null;

dr1 = table1.NewRow();
dr1["TaskID"] = 12;
dr1["GroupID"] = 2;
dr1["GroupName"] = "A";

table1.Rows.Add(dr1);

dr1 = table1.NewRow();
dr1["TaskID"] = 13;
dr1["GroupID"] = 3;
dr1["GroupName"] = "B";

table1.Rows.Add(dr1);

dr1 = table1.NewRow();
dr1["TaskID"] = 14;
dr1["GroupID"] = 2;
dr1["GroupName"] = "A";

table1.Rows.Add(dr1);

dr1 = table1.NewRow();
dr1["TaskID"] = 15;
dr1["GroupID"] = 3;
dr1["GroupName"] = "B";

table1.Rows.Add(dr1);

dr1 = table1.NewRow();
dr1["TaskID"] = 16;
dr1["GroupID"] = 3;
dr1["GroupName"] = "B";

table1.Rows.Add(dr1);

//solution starts from here
Dictionary<string, int> totalCount = new Dictionary<string, int>();

for (int i = 0; i < table1.Rows.Count; i++)
{
    if (totalCount.Keys.Contains(table1.Rows[i]["GroupName"].ToString()))
    {
        int currVal = totalCount[table1.Rows[i]["GroupName"].ToString()];
        totalCount[table1.Rows[i]["GroupName"].ToString()] = currVal + 1;
    }
    else
    {
        totalCount[table1.Rows[i]["GroupName"].ToString()] = 1;
    }
}

foreach (var item in totalCount)
{
    MessageBox.Show(item.Value.ToString());
}

OR

//solution starts from here
var data = table1.AsEnumerable().GroupBy(m => m.Field<string>("GroupName")).Select(grp => new
{
    GroupName = grp.Key,
    Count = (int)grp.Count()
}).ToList();

Hope this helps

Use Linq

   var data=datatable.AsEnumerable().GroupBy(m => m.Field<string>("GROUPNAME")).Select(grp => new
                    {
                        GROUPNAME= grp.Key,
                        Count = (int)grp.Count()
                    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top