user1237131
user1237131

Reputation: 1863

How write query for getting last 3 months records in linq to sql?

I want to display last 3 months sales records.Based on current month will show it's previous records in linq to sql.Please tell me the query for that.

If current month is june then will show apr,may,june records.

id name  no.ofsales  desc          datevalue
1  test    12        test desc     2013-10-12 
2  test1   16        desc message  2013-09-14

Give me idea on this query.

Upvotes: -1

Views: 12240

Answers (3)

David
David

Reputation: 700

var minDate = DateTime.Now.AddMonths(-3);

from x in datatable
where x.datevalue> minDate
select x;

Upvotes: 2

Teeknow
Teeknow

Reputation: 1015

I think something like this could work:

yourCollection.Where(x => 
    DateTime.Compare(x.DateTimeProperty, DateTime.Today.AddMonths(-3)) >= 0);

Upvotes: 1

Bibhu
Bibhu

Reputation: 4081

from x in datatable
where x.datevalue> DateTime.Now.AddMonths(-3) 
orderby x.id ascending
select x;

Upvotes: 1

Related Questions