score:12
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.
score:0
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 ?
score:2
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();
Source: stackoverflow.com
Related Articles
- Group by year and month with given date range using Lambda or LINQ Query
- Groupby multiple date properties by month and year in LINQ
- I wants to date month and year add 2 month later in linq
- Linq query filter by date (Month and year)
- How to group an SQL date column stored as int as Year Month using LINQ
- Group by year descending, order by month and total on another column Linq query
- C# LINQ query to select only first date in a month from List<DateTime>
- Search for month or year in a date using Linq based on condition
- How to get Date Month and Year Number using dataSet.Tables query
- Filter data sets with LINQ by year of a date
- LINQ how to Group data by date day and month and year
- How Filter Binding Source Connected To linq Query
- How to Select Min and Max date values in Linq Query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Linq query to filter id inside a list of list c#
- Use Linq query to compare date only with DateTime field
- LinQ max Date in one Query Optimized
- LINQ TO SQL, Dynamic query with DATE type fields
- LINQ Query - nearest adjacent date
- Filling in missing dates using a linq group by date query
- format date in linq query result
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- LINQ Filter query with multiple optional parameters
- how do i use group by only on date from datetime in LINQ query
- Date range falling between two dates in a LINQ query
- filter a linq query based on the results of another query's results
- linq query distinct date
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- LinQ: C# sort an array while copying with some filters
- .NET Core - Different spatial / geographic results produced in IEnumerable vs IQueryable
- LINQ2SQL doesn't return row if checking with null
- LINQ-to-SQL Joins
- Should LINQ Expressions be used inside foreach loop or not?
- Retrieving a Deeply Nested Value in an XML File
- C# Linq vs. Currying
- Eager Loading in .Net4 Link-to-EF; maybe like DataLoadOptions
- Linq modified get a error message New transaction is not allowed because there are other threads running in the session
- EF MVC connection string and associations
- cannot convert lambda expression to Func<T,TResult> while trying pass around an IQueryable<T>
- Duplicates in results from .ToArray in LinQ query
- Why does linq-2-sql create extra unnecessary objects?
- Hacker News style ordering algorithm in Linq-To-SQL
- GroupBy and IEqualityComparer<TKey> comparer
- C# populate DataGrid with List<List<string>>?
- Using || operator in LinQ
- Linq SELECT with ExecuteQuery
- Linq on XML get value from nested elements in select
- Linq GroupBy Projection with Multiple Groups