2013-08-02 71 views
0

因此,我在一般的LINQ和數據庫中不是很強大,所以請理解我知道我是新手。我試圖從系統中獲得有關員工活動的信息。這是問題,我有4個mysql查詢返回表給我。我得到像這樣當Datatable.Merge無法合併行時,使用Linq合併4個表格

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>'「 +啓動+ 」 '和datetime_init <'「 +端+「」 )和(break.name = '15 Minute')GROUP BY break.name,agent「; mySqlDataAdapter = new MySqlDataAdapter(standard,connection); 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'的引用或使用指令?

編輯:

我不能讓myLinq作爲一個DataTable正確綁定由於匿名數據類型的任何知道我做錯了什麼?

回答

1

嘗試增加以System.Core程序引用和using指令System.Linq的和將System.Data.Linq

+0

好是固定的錯誤,但我如何得到它綁定到表 – rytan2005

+1

結帳此鏈接:http ://stackoverflow.com/questions/15213919/how-to-populate-datatable-with-anonymous-linq-result –