在MVC3中计算查询数组时出错

时间:2013-05-17 12:39:46

标签: c# asp.net-mvc entity-framework

我要做的是模拟将通过网址从另一个系统发送的随机令牌。该其他系统还将在数据库中插入3个值:

  • ID
  • IP
  • 当前时间

现在,在另一个时间,客户端访问应用程序时,它必须具有与首次访问它的系统相同的IP和ID,并且时间差必须小于30分钟,否则会话未经过身份验证,并且机器将被限制并发送到错误页面。

我遇到了问题:

  • 当我执行session[] sessionArray = a.ToArray();时,会出现以下错误:
  

{“FUNCTION cgdimport.DiffMinutes不存在”}

正如WiiMaxx解释的那样:

  

没有你的错误不存在你的错误   EntityFunctions.DiffMinutes(b.start,DateTime.Now)。因为你的b   doesnt包含一个名为DiffMinutes()的函数

所以我尝试了另一种选择:

ar a = (from b in data.session 
where b.idsession == sessionid 
&& b.ip == clientip 
&& DateTime.Now <= b.start + new TimeSpan(0, 0, 30, 0) 
select b);

但它给了我以下错误:

  

DbArithmeticExpression参数必须具有数字公共类型。

另一个:

var a = (from b in data.session
                            where b.idsession == sessionid
                            && b.ip == clientip
                            && DateTime.Now <= b.start.AddMinutes(30)
                            select b);

但是出现了这个错误:

  

LINQ to Entities无法识别方法'System.DateTime   AddMinutes(Double)'方法,这个方法无法翻译成   商店表达。

public ActionResult SessionController()
    {
        var data = new cgdimportEntities();



        //Request sessionnid from the URL
        if (!Request.QueryString["token"].IsEmpty() && Request.QueryString["token"] != null)
        {
            //Obtain client IP 
            string clientip = HttpContext.Request.UserHostAddress;


            //Request sessionid from url
            string sessionid = Request.QueryString["token"];
            //string sessionid = token;

            //Dummy Test Insert

            var sessionvar = new session {idsession = sessionid, ip = clientip, start = DateTime.Now};

            data.session.Add(sessionvar);


            try { data.SaveChanges(); }

            catch (DbEntityValidationException dbEx)
                {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}", validationErrors.Entry.Entity.GetType().FullName,
                                    validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
                }

            //old code
            //var a = (from b in data.session
                        //where b.idsession == sessionid
                        //&& b.ip == clientip
                        //&& EntityFunctions.DiffMinutes(b.start, DateTime.Now) < 30
                        //select b);
            //old code
            //var a = (from b in data.session 
                       //where b.idsession == sessionid 
                       //&& b.ip == clientip 
                       //&& DateTime.Now <= b.start + new TimeSpan(0, 0, 30, 0) 
                       //select b);

            //new code
              var a = (from b in data.session
                         where b.idsession == sessionid
                         && b.ip == clientip
                         && DateTime.Now <= b.start.AddMinutes(30)
                         select b);


            int count = 0;


            //ERROR HERE! ERROR HERE!
            session[] sessionArray = a.ToArray();


            foreach (var i in sessionArray)
            {
                count++;
            }

            //if 0 rows are counted
            if (count == 0)
            {
                Session["authenticated"] = "false";
                return RedirectToAction("Erro", "Login", new { erro = "No rows counted" });
            }
            //Once all queries are verified, the user is authenticated
            Session["authenticated"] = "true";
            return RedirectToAction("Painel", "Data");
        }
            Session["authenticated"] = "false";
            return RedirectToAction("Erro", "Login", new { erro = "Query is null" });
   }

2 个答案:

答案 0 :(得分:2)

使用EntityFunctions添加分钟

ar a = (from b in data.session 
 where b.idsession == sessionid 
 && b.ip == clientip 
 && DateTime.Now <= System.Data.Objects.EntityFunctions.AddMinutes(b.start, 30)
 select b);

答案 1 :(得分:0)

因为DateTime比较的一个参数是一个常量(关于查询),你可以从查询中删除AddMinutes,这应该删除异常:

DateTime nowMinus30Minutes = DateTime.Now.AddMinutes(-30);

var a = (from b in data.session
         where b.idsession == sessionid
            && b.ip == clientip
            && nowMinus30Minutes <= b.start
         select b);