从数据库填充类

时间:2013-05-29 17:52:22

标签: c# class

到目前为止,这是方法。

public static void FillUserListClass()
{    
    List<User> UserList = new List<User>();
    OleDbConnection conn = new OleDbConnection(strAccessConn);
    string query = "SELECT ControlNumber, UserName FROM Log WHERE Log.EndStatus in ('Needs Review', 'Check Search', 'Vision Delivery', 'CA Review', '1TSI To Be Delivered');";

    OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
    DataTable dt = new DataTable();

    try
    {
        adapter.Fill(dt);

        for (int ctr = 1; ctr <= dt.Rows.Count; ctr++)
        {
            UserList.UserName.Add(dt.Columns["UserName"].ToString());
            UserList.ControlNumber.Add(Convert.ToInt32(dt.Columns["ControlNumber"]));
        }
    }
    catch (OleDbException ex)
    {}
    finally
    {
        conn.Close();
    }               
}

我想要做的是抓住ControlNumber&amp; amp; UserName使用我的查询,并为每个UserName创建一个唯一的类。每个用户类

class User
{
    public string UserName { get; set; }
    public List<Int32> ControlNumber { get; set; }
}

将包含用户姓名(用户名)和我使用查询提取的控制号码(controlNumbers)列表。

2 个答案:

答案 0 :(得分:3)

您也可以使用LINQ轻松完成此操作,并且更易于阅读和更简洁:

var users = from c in dt.AsEnumerable()
            group c by c.Field<string>("UserName") into gr
            select new  User { Username=  gr.Key ,
                                ControlNumbers = (from x in dt.AsEnumerable()
                                                  where x.Field<string>("UserName") == gr.Key 
                                                  select x.Field<int>("ControlNumber")).ToList() };

更新

List<User> userList = (from c in dt.AsEnumerable()
                group c by c.Field<string>("UserName") into gr
                select 
                new User { 
                           Username=  gr.Key ,
                           ControlNumbers = 
                           (from x in dt.AsEnumerable()
                           where x.Field<string>("UserName") == gr.Key 
                           select x.Field<int>("ControlNumber")).ToList() 
                          }
                        ).ToList();

基本上,您正在获取数据表,按UserName分组并使用此唯一用户名列表再次查询数据表,以读取与当前UserName相关的所有控制号。

答案 1 :(得分:1)

你可以像这样使用Dictionary

public static void FillUserListClass()
{
    Dictionary<string, User> UserList = new Dictionary<string, User>();
    OleDbConnection conn = new OleDbConnection(strAccessConn);
    string query = "SELECT ControlNumber, UserName FROM Log WHERE Log.EndStatus in ('Needs Review', 'Check Search', 'Vision Delivery', 'CA Review', '1TSI To Be Delivered');";

    OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
    DataTable dt = new DataTable();

    try
    {
        adapter.Fill(dt);

        for (int ctr = 1; ctr <= dt.Rows.Count; ctr++)
        {
            var row = dt.Rows[ctr];
            var userName = row["UserName"].ToString();
            if (!UserList.ContainsKey(userName))
            {
                UserList.Add(userName, new User()
                {
                    UserName = userName,
                    ControlNumber = new List<int>()
                });
            }
            UserList[userName].ControlNumber.Add((int)row["ControlNumber"]);
        }
    }
    catch (OleDbException ex)
    {

    }
    finally
    {
        conn.Close();
    }
}