简体   繁体   中英

How to format the datatable in C#?

I have a datatable like this.

 User       Unit      Score
 kiran      testing     2
 kiran      demo        1
 kiran      TRacking    3.5
 manu       1234       .5
 manu        1567       3.5
 Priya       1689       9
 Tom         1567       2
 Tom         3454       5
 Peter       testing    2
 Peter       demo       2.5

Now I need result like this in another datatable:

   User       Svc     VSS
   kiran      6.5
   manu               4.0
   Priya      9.0
   Tom                7.0
   Peter      4.5

I need to check for user their unit, based on that I need to sum up the Score. If the unit contains string then those score value should go to a new datatable column called svc.

If the unit contains Integer then those score value should go to a new column called vss

So that the final result will be like this:

       User       Svc     VSS
       kiran      6.5
       manu               4.0
       Priya      9.0
       Tom                7.0
       Peter      4.5

I am new to C#.

Here is a solution, although keep in mind DataTables are awkward to work with and it's better to create a collection of objects to work with instead.

public class SvcVssRecord
{
    public SvcVssRecord(double svc, double vss)
    {
        Svc = svc;
        Vss = vss;
    }

    public double Svc { get; set; }
    public double Vss { get; set; }
}

public class Builder
{
    private readonly Dictionary<string, SvcVssRecord> _records;

    public Builder()
    {
        _records = new Dictionary<string, SvcVssRecord>();
    }

    public void Add(string user, string unit, double score)
    {
        int result;
        bool scoreIsSvc = !int.TryParse(unit, out result);

        if (!_records.ContainsKey(user))
        {
            _records.Add(user, new SvcVssRecord(scoreIsSvc ? score : 0, scoreIsSvc ? 0 : score));
        }
        else
        {
            if (scoreIsSvc)
            {
                _records[user].Svc += score;
            }
            else
            {
                _records[user].Vss += score;
            }

        }
    }

    public DataTable ToDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("User"));
        dt.Columns.Add(new DataColumn("Svc"));
        dt.Columns.Add(new DataColumn("Vss"));

        foreach(var key in _records.Keys)
        {
            DataRow dr = dt.NewRow();
            dr["User"] = key;
            dr["Svc"] = _records[key].Svc;
            dr["Vss"] = _records[key].Vss;
            dt.Rows.Add(dr);
        }

        return dt;
    }
}

public class Run
{
    public void RunApp(DataTable source)
    {
        Builder b = new Builder();

        foreach (DataRow dr in source.Rows)
        {
            b.Add((string) dr["User"], (string) dr["Unit"], (double) dr["Score"]);
        }

        DataTable dt = b.ToDataTable();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM