【问题标题】:DateTime in linqlinq中的日期时间
【发布时间】:2013-03-15 07:03:55
【问题描述】:

所以我试图从脚本任务内的表中获取最新记录。错误出现在 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

【问题讨论】:

  • r.TimeStamp 在数据库和模型中的类型是什么?
  • 数据库中的类型为DateTime,包中的类型为DateTime。我不确定你所说的模型是什么意思?
  • 您是否可以在不指定Max() 的情况下从r.TimeStamp 中选择所有日期的列表?
  • 是的,如果我不让 Max 它运行,我可以做到这一点

标签: c# sql-server-2008 datetime linq-to-sql ssis


【解决方案1】:

如果Max 不起作用,那该怎么办:

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

【讨论】:

  • First 或 default 是我尝试的第一件事,它给了我一个“没有支持的 SQL 转换”错误
  • @Mike_L 只是好奇,如果您将 [Column] 属性放在属性 TimeStamp 而不是字段 timeStamp 上,它会运行吗?
【解决方案2】:

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

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

【讨论】:

  • 这里也一样。 'SQL 中不支持 Transaltion'
【解决方案3】:

你可以试试 OrderBy

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

【讨论】:

  • @Mike_L 他快一点 :) 所以它给了你对 FirstOrDefault 的例外?
  • 是的,我的意思是我在发帖之前就已经尝试过了。是的,它为 FirstOrDefault 提供了一个例外。我也尝试了选择中的一些 Convert.ToDateTime 和 DateTime.Date
  • @Mike_L 这东西有用吗? records.ToList().OrderByDescending(r=>r.TimeStamp).Select(r=>r.TimeStamp).FirstOrDefault();你能把你的记录转换成列表吗?
  • 实际上我在这里遇到了一个语法错误,它并不认为 FirstOrDefault 它在哪里
  • @Mike_L 我想您必须将结果集转换为集合类型。没有你的模型很难给你写一个合适的例子。但我建议您在选择部分拆分 Linq,将其分配给某个 var(它应该具有 IQuerable 类型),然后尝试将此 var 转换为列表并在此集合上创建 Max 或 OrderBy。
【解决方案4】:

可能是因为这个:

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();

【讨论】:

  • 出于某种原因,这使得 ssis 完全停止工作,什么也不做。脚本任务只是保持黄色。
  • 但它不起作用。 SSIS 停止工作,出现一个对话框并停止 SSIS。
猜你喜欢
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
相关资源
最近更新 更多