2013-03-15 113 views
1

所以我試圖從腳本任務中的表中獲取最近的記錄。該錯誤是在未來的LINQ查詢,它看起來像:在linq的日期時間

"Could not Translate expression 'Table(SSASLogging).Select(r=>r.TimeStamp).Max()' into SQL and could not treat it as a local expression."

問題在於DateTime數據類型,但我需要的日期時間把它給我的SSIS變量。我知道這可以在一個執行SQL任務中輕鬆完成,但是我現在太放棄了!我知道有一些用於DateTime的LinqToSQL方法,但它們用於比較它看起來像,我不知道如何在這裏應用它們。

public DateTime getLatest() 
    { 
     DateTime result = new DateTime(); 

     //temp dummy/defaul date is two days ago 
     result = DateTime.Now.AddDays(-2); 

     try 
     { 
      //get the data connection string from the connection manager 
      RW = (string)Dts.Connections["ReportingWarehouse"].ConnectionString; 

      //Remove the Provider, Auto Translate, and Application 
      //as it is not a parameter for the DataContext constructor 
      RW = RW.Remove(RW.IndexOf("Provider=SQLNCLI10.1;"), "Provider=SQLNCLI10.1;".Length); 
      RW = RW.Remove(RW.IndexOf("Auto Translate=False;"), "Provider=SQLNCLI10.1;".Length); 
      RW = RW.Remove(RW.IndexOf("Application"),RW.Length - RW.IndexOf("Application")); 



      MessageBox.Show(RW); 

      //get the last insertion date from the SSASLoging table 
      using (DataContext RWData = new DataContext(RW)) 
      { 
       Table<SSASLogging> records = RWData.GetTable<SSASLogging>(); 
       var rs = (from r in records 
         select r.TimeStamp).Max(); 
       //result = rs.FirstOrDefault(); 
      } 

     } 

     catch (Exception e) 
     { 
      MessageBox.Show("Exception in Retrieving latesttime" + e.Message + "/n" 
          + e.StackTrace); 
     } 

     return result; 
    } 

}//end partial class 


[Table] 
public class SSASLogging 
{ 
    [Column(Name = "CREATED_TIMESTAMP")] 
    private DateTime timeStamp; 

    public DateTime TimeStamp 
    { 
     get { return this.TimeStamp; } 
    } 
}//End SSASLogging 
+2

數據庫和模型中的'r.TimeStamp'類型是什麼? – MarcinJuraszek 2013-03-15 07:07:07

+0

數據庫中的類型爲DateTime,包中的類型爲DateTime。我不確定你的模型是什麼意思? – 2013-03-15 07:13:32

+0

您是否可以從'r.TimeStamp'中選擇所有日期的列表,而不指定'Max()'? – MarcinJuraszek 2013-03-15 07:17:24

回答

1

嘗試排序表和Take(1).ToList()。它應該返回List 0或1個元素。然後你可以使用FirstOrDefault,這將通過應用程序不受SQL執行,:

var rs = (from r in records 
      orderby r.TimeStamp descending 
      select r).Take(1).ToList().FirstOrDefault(); 
+0

同樣的交易在這裏。 '不支持SQL中的Transaltion' – 2013-03-15 07:30:58

2

如果Max不工作,你看這個:

var maxDate = 
    (from r in records 
    orderby r.TimeStamp descending 
    select r.TimeStamp) 
    .FirstOrDefault(); 
+0

+1我會做同樣的事情。 – 2013-03-15 07:08:30

+0

首先或默認是我嘗試的第一件事,它給了我'沒有支持的翻譯到SQL'錯誤 – 2013-03-15 07:12:32

+1

@Mike_L只是好奇,如果你把'[Column]'屬性放在'TimeStamp'屬性上而不是字段'timeStamp',它運行嗎? – 2013-03-15 13:35:43

1

您可以嘗試排序依據

var rs = (from r in records orderby r.TimeStamp descending 
        select r).FirstOrDefault(); 
+0

多數民衆贊成在樓上的人說,但我第一次嘗試 – 2013-03-15 07:13:59

+0

@Mike_L他有點快:)所以它給你的例外FirstOrDefault? – Alex 2013-03-15 07:19:42

+0

是的我的意思是,我甚至在我甚至在這裏發佈之前都試過。是的,它給FirstOrDefault一個例外。我嘗試了一些Convert.ToDateTime內部的選擇和DateTime.Date – 2013-03-15 07:21:23

0

這可能是因爲這一點:

http://msdn.microsoft.com/en-us/vstudio/ff963710.aspx

這樣:

var rs = (from r in records.AsEnumerable() 
     select r.TimeStamp).Max(); 

或:

var rs = (from r in records 
     select r.TimeStamp).AsEnumerable().Max(); 
+0

由於某些原因,這使得ssis完全癱瘓並且什麼都不做。腳本任務只是保持黃色。 – 2013-03-15 15:55:32

+0

拋出異常嗎? – Arie 2013-03-18 07:52:37

+0

沒有例外拋出 – 2013-03-19 16:35:54