Looks like you should be able to do:

// Type and property names changed to match .NET conventions
public IEnumerable<Content> GetContents(int year, int month)
    return db.Contents // Or wherever you're getting data from
             .Where(c => c.Date.Year == year && c.Date.Month == month);

I've split out year and month as separate parameters, as that's how you described them - if you really want to be able to handle "01/2013" you would probably want to just split by '/' and parsing each of the pieces as an integer first. (Alternatively, parse it as a date in a specific format and then take the year and month from that.)

EDIT: Note that formatting each value as a string is pointless and potentially incorrect unless you're careful. (For example, unless you specify the culture explicitly, if you use / in the pattern you'll get the current culture's date separator, which may well not be what you expect.)

You're not actually trying to match a string pattern - you're trying to test for the year and month of the date. That's what the code above expresses clearly. String formatting is an irrelevance.


I suddenly worked out like below:

public IEnumerable<Content> GetContents(string date) /* E.G: date = '07/2013' */
    return db.Contents // Or wherever you're getting data from
             .Where(c => c.Date.ToString("MM/yyyy") == date);

Is that right way to do ?


This is correct for only moth and year

 List<eTransaction> lsttrans = db.eTransactions.Where(c => c.SchemeID == scid && 
    c.DateOfPay.Value.Month == month && c.DateOfPay.Value.Year == year).ToList();

Related Articles