SolvedPrivate

using linq in C# to get DateTime range from list

Posted on 2014-09-17
3
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

691 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