Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.Net MVC LINQ查询以检查两个日期之间是否已存在协议_Asp.net_Asp.net Mvc_Linq_Asp.net Mvc 4 - Fatal编程技术网

Asp.Net MVC LINQ查询以检查两个日期之间是否已存在协议

Asp.Net MVC LINQ查询以检查两个日期之间是否已存在协议,asp.net,asp.net-mvc,linq,asp.net-mvc-4,Asp.net,Asp.net Mvc,Linq,Asp.net Mvc 4,我有一个Asp.NETMVC5项目,我需要一些帮助。逻辑是客户需要有一个协议。这很好,因为我可以创建第一个协议。我遇到的问题是,如果需要为客户创建另一个协议,它不能与现有协议在同一期限内。最初,我考虑在Create方法的Agreement控制器中放置一些东西来执行检查。 我现在拥有的是 控制器: // GET: Agreements/Create public ActionResult Create(int? Id, string Name) { if (Id

我有一个Asp.NETMVC5项目,我需要一些帮助。逻辑是客户需要有一个协议。这很好,因为我可以创建第一个协议。我遇到的问题是,如果需要为客户创建另一个协议,它不能与现有协议在同一期限内。最初,我考虑在Create方法的Agreement控制器中放置一些东西来执行检查。 我现在拥有的是

控制器:

 // GET: Agreements/Create
    public ActionResult Create(int? Id, string Name)
    {

        if (Id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Agreement model = new Agreement();
       // model.FullName = Name;
        model.CustomerId = Id;

        ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name");
        ViewBag.SupplierId = new SelectList(db.Suppliers, "SupplierId", "Name");

        return View(model);
    }

    // POST: Agreements/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "AgreementId,CustomerId,CustomerHasAgreement,SupplierId,Start,End,AgreementPlanId,AgreementType")] Agreement agreement)
    {

        if (ModelState.IsValid)
        {
            var bExist = db.Agreements.Any(s => (s.Start <= agreement.End && s.End >= agreement.Start));    //or your check logic
            if (!bExist)
            { 
                db.Agreements.Add(agreement);
                db.SaveChanges();
                return RedirectToAction("Details", "Agreements", new { id = agreement.AgreementId });
            }
            else 
                ModelState.AddModelError("Error", "Sorry something happened unexpectedly !!!");
                return RedirectToAction("Index");

        }

        ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name", agreement.CustomerId);
        ViewBag.SupplierId = new SelectList(db.Suppliers, "SupplierId", "Name", agreement.SupplierId);
        return View(agreement);
    }
我尝试使用LINQ语句检查新协议是否大于旧协议的结束日期

 var bExist = db.Agreements.Any(s => (s.Start <= agreement.End && s.End >= agreement.Start));    //or your check logic
var bExist=db.Agreements.Any(s=>(s.Start=agreement.Start))//还是你的支票逻辑
这似乎只是跳过if语句,而不是插入记录

谁能帮帮我吗。我已经试过阅读微软教程 但在我的解决方案中尝试实现它时,似乎总是遇到困难

非常感谢


Mark

@markabarmi执行OR运算符。您正在查找2个可能问题中的1个或2个。不需要两者兼而有之就可以触发bExists为真。还可以将该结果集限制为您的特定客户

var bExist = db.Agreements.Any(s => (s.CustomerId == agreement.CustomerId) && ((s.Start <= agreement.End) || (s.End >= agreement.Start)));
var bExist=db.Agreements.Any(s=>(s.CustomerId==agreement.CustomerId)&((s.Start=agreement.Start));

谢谢@mwwallace8的回复,我已经试过了,但是它还是跳过了,直接跳到了我的
else
语句中,即使客户甚至没有协议。@markabarmi。。。你知道吗。您可能需要添加参数,以确保您看到的是正确的客户。我把它添加到lambda中以反映我的意思。在没有customerId限制结果集的情况下,您将返回不相关的记录。感谢@mwwallace8,您需要为客户添加参数。使用你的答案,它似乎是工作:)谢谢你
var bExist = db.Agreements.Any(s => (s.CustomerId == agreement.CustomerId) && ((s.Start <= agreement.End) || (s.End >= agreement.Start)));