Solved

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

Posted on 2011-02-23
1
853 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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 33
Select all rows in datagridview 15 18
Paging GridView 7 32
Get Count in a variable with 5 digits always 1 8
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

10 Experts available now in Live!

Get 1:1 Help Now