SolvedPrivate

using linq in C# to get DateTime range from list

Posted on 2014-09-17
3
41 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON response syntax 3 105
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 136
wordlen challenge 3 66
Problem to picture file 20 49
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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