简体   繁体   English

Linq比较日期时间值

[英]Linq compare datetime values

I have dateTime value start and finish date. 我有dateTime值的startfinish日期。 I compare it from old values. 我将其与旧的价值观进行比较。 If it is exist i dont let to update. 如果存在,我不让更新。 Value properties in entity it is int and client it is dateTime . entity值属性为intclient值属性为dateTime Linq code is; Linq代码是;

  public static bool IsWROExist(CRO cROne)
        {
            Entities entities = new Entities();
            int startDate = Convert.ToInt32(cROne.StartDate);
            int finishDate = Convert.ToInt32(cROne.FinishDate);

            return (from WRO in entities.WRO
                    where WRO.id != cROne.Id &&
                          (WRO.startDate == startDate &&
                              WRO.finishDate == finishDate )
                    select WRO).Any();
        }

I am getting this error : Input string was not in a correct format. 我收到此错误消息:输入字符串的格式不正确。

It appears that you're not using the variables that contain the start and end date. 看来您没有使用包含开始日期和结束日期的变量。

The following is what you should have 以下是您应该拥有的

                     && (WRO.startDate == startDate &&
                          WRO.finishDate == finishDate )

You have added the && twice: 您已两次添加&&

 return (from WRO in entities.WRO
                where WRO.id != cROne.Id &&    <-------
            ------>      && (WRO.startDate == startDate &&
                          WRO.finishDate == finishDate )
                select WRO).Any();

Try this way to convert the date time: 尝试这种方式转换日期时间:

int n = int.Parse(date.ToString("yyyyMMddHHmmss"));

and then compare with the value. 然后与该值进行比较。

!!warning: int may not hold the value fully. !!警告:int可能无法完全保留该值。 you might want to use long data type. 您可能要使用长数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM