LINQ question

Hello EE ...  I got a linq expression in like this :

  Dim q = (From m In MyItems
                    Where arrString.All(Function(v) m.Value.Contains(v))
                    Order By m.Key Descending
                    Select m)

the first item in ever rows in q is a datevalue.

How could I modify that LINQ expression so that in q I would get the number of days difference between each rows instead of every datevalue ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi PhilippeRenaud;

You will not be able to do it in the Linq query itself because Linq will not know what the next date will be until after it has already created/processed the current object.

What you can do is first apply the ToList() method to the query, like this:
Dim q = (From m In MyItems
                Where arrString.All(Function(v) m.Value.Contains(v))
                Order By m.Key Descending
                Select m).ToList()

Open in new window

this is because Linq is a deferred execution operation, meaning it does not create a result set until it has been enumerated over or use a method such as ToList. Then you can use a For loop and for each iteration use DateDiff on the current object and access the next by using the current index plus 1. Depending on the data type of the object holding the date you might have to add an extra property to the object to hold the number of days, because of the date is a DateTime object it will not hold an integer.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PhilippeRenaudAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.