2013-03-15 88 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