1

这是迄今为止的方法。

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 和 UserName,并为每个 UserName 创建一个唯一的类。每个用户类别

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

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

4

2 回答 2

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();

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

于 2013-05-29T18:25:00.217 回答
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();
    }
}
于 2013-05-29T17:57:50.753 回答