English 中文(简体)
如何在C#中编制数据表?
原标题:How to format the datatable in C#?
  • 时间:2010-07-21 04:05:30
  •  标签:
  • c#

我有这样的数据表。

 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

现在,我需要在另一个数据表中得出这样的结果:

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

我需要对用户单位进行检查,因为我需要总结记分。 如果该单位包含插图,这些记分值应进入一个称为vc的新数据表栏。

如果该单位有, 然后,这些记分值应归入一个称为“v”的新栏。

因此,最终结果就是这样:

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

我是C#的新成员。

最佳回答

这里是一种解决办法,但铭记数据交换器是同我们合作的关键所在,而且它更有助于建立一套与他人合作的物体。

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();
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签