asp.net mvc 3 - Linq query filter by date (Month and year) -
public class content { public int id { get; set; } public string name { get; set; } public datetime date { get; set; } } id name date 1 content1 01/01/2013 2 content2 05/01/2013 3 content3 05/03/2013 4 content4 01/06/2013 5 content5 10/03/2012 6 content6 01/01/2012
i'm trying if query passes '01/2013', query should return content id 1,2 . there knows how query above situation ?
looks should able do:
// type , property names changed match .net conventions public ienumerable<content> getcontents(int year, int month) { return db.contents // or wherever you're getting data .where(c => c.date.year == year && c.date.month == month); }
i've split out year
, month
separate parameters, that's how described them - if want able handle "01/2013" want split '/'
, parsing each of pieces integer first. (alternatively, parse date in specific format , take year , month that.)
edit: note formatting each value string pointless , potentially incorrect unless you're careful. (for example, unless specify culture explicitly, if use /
in pattern you'll current culture's date separator, may not expect.)
you're not trying match string pattern - you're trying test year , month of date. that's code above expresses clearly. string formatting irrelevance.
Comments
Post a Comment