Ilaria
Ilaria

Reputation: 187

Linq compare datetime values

I have dateTime value start and finish date. 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. Linq code is;

  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.

Upvotes: 0

Views: 635

Answers (3)

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. you might want to use long data type.

Upvotes: 1

Jeroen van Langen
Jeroen van Langen

Reputation: 22083

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

Upvotes: 1

Richard Harrison
Richard Harrison

Reputation: 19403

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 )

Upvotes: 1

Related Questions