开发者

Need an efficient way for a simple problem in c#

开发者 https://www.devze.com 2023-01-24 08:30 出处:网络
I am developing a web application using c#. I am reading some data from database to a data table named dt . Sample data is shown below.

I am developing a web application using c#. I am reading some data from database to a data table named dt . Sample data is shown below.

agentName  WeekNumber Score 
John       3          45
John       5          78
John       6          33

I want to make some change in the above data based on some conditions. Cause i 开发者_如何学Gowant to draw a graph based on this data.The agent name is always same.and the week field is unique. I want to make a new data table using conditions listed below.

1-The new data table week field must start from 1.If there is no entry in the data table for week 1, you can add the data into new data table as shown below

John 1  0

it means just give 0 as score.

2-If there is any missing weeks from second row onwards in the data table obtained from database , add the row for that week with score as -1. in the above example after first row there is a missing week 4. so add it to new data table with score -1

the new data tab;e for sample data is shon below

agentName WeekNumber Score
John      1          0
John      2          0
John      3          45
John      4         -1
John      5         78
John      6         33

How can i do it in an efficient method using c#? The number of rows will vary . I want to do it using c# . not by using queries because of some reason


Have a table in the database with default values for every week in a year. Left Join on this table when selecting out your resuls and take the default value if the other is missing.

e.g.

select 
    dt.name, 
    def.week_no, 
    (case dt.value is null then def.value else dt value end) value
from 
    def left join dt on def.week_no = dt.week_no

Code untested, but should take everything from the default table, and rows from dt table where they exist. If the dt row doesn't exist it will take the default value.

Where dt is your existing table with values, and def is the table with a default value for each week.


you can use the following class and the build a new table from it.

class AgentsData
{
    public static DataTabe ProcessDataTabe(DataTable dt)
    {
        Dictionary<string, AgentData> data = new Dictionary<string, AgentData>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string name = dt.rows[i][0].ToString();
            if (!data.ContainsKey(name))
                data.Add(name, new AgentData);
            int week = Convert.ToInt32(dt.rows[i][1]);
            int score = Convert.ToInt32(dt.rows[i][2]);

            data[name].Add(week, score);
        }

        foreach (vat agentData in data.Values)
            agentData.Process();

        //now build new data table from dictionary and return it
    }   
}

class AgentData
{
    public AgentData(string name)
    {
        Name = name;
        WeekScore = new Dictionary<int,int>();
    }

    public void Add(int weekNumber, int score)
    {
        WeekScore[weekNumber] = score;
    }

    public void Process()
    {
        int min = WeekScore.Keys.Min();
        int max = WeekScore.Keys.Max();

        for (int i = 0; i < min; i++)
            WeekScore.Add(i, 0);

        for (int i = min + 1; i < max; i++)
            if (!WeekScore.ContainsKey(i))
                WeekScore.Add(i, -1);
    }

    public string Name {get; private set;}  
    public Dictionary<int, int> WeekScore { get; private set;}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消