Solved

How would I use a TimeSpan Duration in a LINQ Where clause

Posted on 2011-02-23
1
862 Views
Last Modified: 2012-05-11
I have a LINQ statement where I need to find the first row in a dictionary where the difference between a Dictionary item date and a reference time is less than 2 minutes.

 legSlot = (From l As MissionLeg In missionCollection(missionKey)
                 Where l.DestIcao = eventRef.GeoId And 
                (l.DepartTime - eventRef.EventDateTime.AddHours(-eventRef.FlightTime)) < TimeSpan.FromMinutes(2)
                 Select l).FirstOrDefault()

Open in new window


The code above works fine when l.DepartTime is greater than eventRef.EventDateTime.AddHours(-eventRef.FlightTime))

I tried the following modification:

  (l.DepartTime - eventRef.EventDateTime.AddHours(-eventRef.FlightTime)).Value.Duration() < TimeSpan.FromMinutes(2)

Open in new window


But this throws errors.

Is there another way to get the absolute value of the duration between two dates?


0
Comment
Question by:MDKIMZEY
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34965062
Hi MDKIMZEY;

Try it like this.

legSlot = (From l As MissionLeg In missionCollection(missionKey)
           Where l.DestIcao = eventRef.GeoId And 
                 (TimeSpan.FromTicks(l.DepartTime.Ticks - eventRef.EventDateTime.AddHours(-eventRef.FlightTime).Ticks)) < TimeSpan.FromMinutes(2)
                 Select l).FirstOrDefault()

Open in new window


Fernando
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now