Solved

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

Posted on 2011-02-23
1
875 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 63

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

831 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