1

所以我试图从脚本任务中的表中获取最新记录。错误出现在 Linq 查询中,如下所示:

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

问题在于 DateTime 数据类型,但我需要 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
4

4 回答 4

2

如果Max不工作,这个怎么办:

var maxDate =
    (from r in records
     orderby r.TimeStamp descending
     select r.TimeStamp)
    .FirstOrDefault();
于 2013-03-15T07:07:29.270 回答
1

你可以试试 OrderBy

var rs = (from r in records  orderby r.TimeStamp descending
                     select r).FirstOrDefault();
于 2013-03-15T07:08:33.420 回答
1

尝试对表格和Take(1).ToList(). 它应该返回List0 或 1 个元素。然后您可以使用FirstOrDefault,这将由应用程序执行,而不是由 SQL 执行:

var rs = (from r in records
          orderby r.TimeStamp descending
          select r).Take(1).ToList().FirstOrDefault();
于 2013-03-15T07:26:04.287 回答
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();
于 2013-03-15T07:48:11.353 回答