当Datatable.Merge无法合并行时,使用Linq合并4个表

时间:2013-08-02 16:14:59

标签: c# linq datatable datarow

所以我在LINQ和数据库方面不是很强,所以请理解我知道我是新手。我试图从系统中获取有关员工活动的信息。这是问题,我有4个mysql查询返回表给我。我得到了这样的信息

private DataTable GetBreaks()
    {
        server = "someip";
        database = "calls";
        uid = "user";
        password = "password";
        int port = 999999;
        string connectionString;
        connectionString = "server=" + server + ";" + "database=" + database + ";" + "port=3306;" + "user=" + uid + ";" + "password=" + password + ";";

        connection = new MySqlConnection(connectionString);

        if (this.OpenConnection() == true)
        {
            DataSet dt = new DataSet();
            DateTime endDateRun = radDateTimePicker2.Value.Date;
            DateTime endDate;
            DateTime startDate = radDateTimePicker1.Value.Date;
            if (endDateRun != startDate)
            {

                do
                {
                    endDate = startDate.AddDays(1);
                    string start = startDate.ToString("yyyy-MM-dd HH:mm:ss");
                    string end = endDate.ToString("yyyy-MM-dd HH:mm:ss");
                    string standard = "SELECT number, agent, break, (SUM(TIME_TO_SEC(duration))) AS Total FROM agentBreaks WHERE (datetime_init > '+ start +' and datetime_init < '+ end+' ) and (break= '15 Minute') GROUP BY break,agent

”                             +“加入休息时间(audit.id_break = break.id)其中(datetime_init&gt;'”+ start +“'和datetime_init&lt;'”+ end +“')和(break.name = '15 Minute')GROUP BY break.name,agent“;                         mySqlDataAdapter = new MySqlDataAdapter(标准,连接);                         DataSet DS = new DataSet();                         mySqlDataAdapter.Fill(DS);

                    DS.Tables[0].Columns.Add(new DataColumn("date"));
                    for (int i = 0; i <= DS.Tables[0].Rows.Count - 1; i++)
                    {
                        DS.Tables[0].Rows[i]["date"] = startDate.ToString();
                    }
                    dt.Merge(DS);
                    startDate = startDate.AddDays(1);
                } while (endDateRun != endDate);

            }
            //close connection
            this.CloseConnection();
            return dt.Tables[0];
        }
        return null;
    }

然后我得到其他结果并尝试使用Linq加入它们:

 protected void CreateUnifiedTable()
    {
        DataTable Merged;
        DataTable dt1 = GetClockin();
        DataTable dt2 = GetLunch();
        DataTable dt3 = GetBreaks();
        DataTable dt4 = GetOther();
        var myLINQ = from DataTable t1 in dt1.AsEnumerable()
                     join t2 in dt2.AsEnumerable() on new { t1.id_agent, t1.date } equals new { t2.id_agent, t2.Columnsdate }
                     join t3 in dt3.AsEnumerable() on new { t1.id_agent, t1.date } equals new { t3.id_agent, t3.Columnsdate }
                     join t4 in dt4.AsEnumerable() on new { t1.id_agent, t1.date } equals new { t4.id_agent, t4.Columnsdate }


                     select new
                     {
                         name = t1.Field<string>("agent"),
                         login = t1.Field<DateTime>("Login"),
                         fifteenMinute = t3.Field<TimeSpan>("Total"),
                         lunch = t2.Field<TimeSpan>("Total"),
                         other = t4.Field<TimeSpan>("Total"),
                         logout = t1.Field<TimeSpan>("Logout"),
                         total = t1.Field<TimeSpan>("Total") - t2.Field<TimeSpan> "Total")
                     };
    }

但我得到的只是这个错误 找不到源类型'System.Data.EnumerableRowCollection'的查询模式的实现。找不到“加入”。您是否缺少'System.Linq'的引用或using指令?

编辑:

我无法让myLinq作为DataTable正确绑定,因为匿名数据类型知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试添加对System.Core的引用以及System.Linq和System.Data.Linq的using指令