Asp.Net MVC5 Linq查询检查表中的数据

时间:2016-03-30 15:17:00

标签: c# asp.net linq

我正在尝试编写一个LINQ查询,该查询将检查表中的数据与用户估算的值。这样做的目的是供应商将分配一个计划,其中包含他们必须完成针对该用户的时间。这是通过计划模型和控制器完成的。然后供应商必须完成他们对用户完成的实际时间。在此阶段,供应商正在填写时间表,因此我希望检查时间是否大于分配给他们的时间。

我尝试使用以下查询

 var Time = db.Plans.Any(s => (s.CustomerId == timeSheet.CustomerId) && ((s.Monday <= plan.Monday)));

但这似乎不起作用

我的模特是:时间表

  public class TimeSheet
{
    public int TimeSheetId { get; set; }
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/M/yy}")]
    public DateTime? WeekCommencing { get; set; }
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/M/yy}")]
    public DateTime? WeekEnding { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Monday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Tuesday{ get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Wednesday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Thursday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Friday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Saturday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Sunday { get; set; }

    public int? CustomerId { get; set; }
    public virtual Customer customer { get; set; }

    public bool CompletedTimeSheet { get; set; }

计划模型:

public class Plan
{
    [Key]
    public int PlanId { get; set; }
    [DataType(DataType.Time)]
    [Range(typeof(TimeSpan), "00:00", "12:00")]
    [DisplayFormat(DataFormatString = @"{0:hh\:mm\:ss}")]
    public TimeSpan? Monday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Tuesday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Wednesday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Thursday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Friday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Saturday { get; set; }
    [DataType(DataType.Time)]
    public TimeSpan Sunday { get; set; }

    public int CustomerId { get; set; }
    public int AgreementId { get; set; }

    [HiddenInput(DisplayValue = false)]
    public bool CustomerHasPlan { get; set; }

时间表控制器:

    // GET: TimeSheets/Create
    public ActionResult Create(int? ID)
    {
        if(ID == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        TimeSheet model = new TimeSheet();
        // model.FullName = Name;
        model.CustomerId = ID;
        return View(model);
    }


    // POST: TimeSheets/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 = "TimeSheetId,WeekCommencing,WeekEnding,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,CompletedTimeSheet,CustomerId,Total")] TimeSheet timeSheet, Plan plan)
    {

        if (ModelState.IsValid)
        {
            var Monday_test = db.TimeSheets.Any(s => (s.CustomerId == timeSheet.CustomerId) && (s.Monday <= timeSheet.Monday));
            if (!Monday_test)
            {

                db.TimeSheets.Add(timeSheet);
                db.SaveChanges();
                return RedirectToAction("Index");

            }

            else
            {
                return View("Error");
            }
        }

        return View(timeSheet);
    }

希望有人能看到我出错了。

由于

1 个答案:

答案 0 :(得分:0)

无法确切地告诉您查询的内容,但通过查看您的查询并将其与模型进行比较,您在星期一错误拼写为&#34; Maonday&#34;。

尝试:

 var Time = db.Plans.Any(s => (s.CustomerId == timeSheet.CustomerId) && ((s.Monday <= plan.Monday)));