SolvedPrivate

using linq in C# to get DateTime range from list

Posted on 2014-09-17
3
40 Views
Last Modified: 2016-02-15
I am using linq(in c#) to get the records going back 1 year back. My code below works if i use .AddMonths(-10);  but if I used AddMonths(-12); all the records are returned. I'm not sure why this occurs.  

Could someone tell how can i get the previous 12 month using linq.  




                  var result = items
                    .Where(x => !x.IsDeleted)
                    .OrderByDescending(x => x.OpenedAt)                  
                    .Take(perPage)
                    .ToList();




                var startDate1 = DateTime.Now.AddMonths(-10);              
                var newResult1 = result.Where(x => x.CreatedAt >= startDate1);


                var startDate2 = DateTime.Now.AddMonths(-12);              
                var newResult2 = result.Where(x => x.CreatedAt >= startDate2);
0
Comment
Question by:bojeff30
  • 2
3 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40330983
What are the values of x.CreatedAt that are not evaluating correctly, can you post some examples?
0
 

Accepted Solution

by:
bojeff30 earned 0 total points
ID: 40333112
turnsout the data received was incorrect - thanks for the reply.
0
 

Author Closing Comment

by:bojeff30
ID: 40348356
issue due to bad data
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

Suggested Solutions

Title # Comments Views Activity
seriesUp challenge 7 130
splitOdd10 challenge 5 80
groovy example issue 10 90
Interview question Javascript, database 12 32
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

895 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

13 Experts available now in Live!

Get 1:1 Help Now