score:2
I'd try this:
if (tb_DateFrom.Text != "") {
journeys = from j in journeys
where j.DateTo.CompareTo(tb_DateFrom.Text) >= 0
select j;
}
if (tb_DateTo.Text != "") {
journeys = from j in journeys
where j.DateFrom.CompareTo(tb_DateTo.Text) <= 0
select j;
}
score:0
As the string format you have sorts in the same order as the dates they represent, I don't see why you have to convert their data format at all. Just do (untested):
journeys = from j in journeys
where j.DateTo >= tb_DateFrom.Text && j.DateFrom >= tb_DateTo.Text
select j;
Update, after Joakim's comment, still just using the sort order of the strings:
journeys = from j in journeys
where j.DateTo.CompareTo(tb_DateFrom.Text) >= 0 &&
j.DateFrom.CompareTo(tb_DateTo.Text) <= 0
select j;
(Det borde väl fungera, Joakim?)
Oops, I missed the accepted answer, but I'll still leave my first edit...
score:1
Why don't you convert textbox values to datetime and then compare the dates in the where clause, instead of converting to int
DateTime? dateFrom = null, dateTo = null;
if(!String.IsNullOrWhiteSpace(tb_DateFrom.Text))
dateFrom = DateTime.ParseExact(tb_DateFrom.Text, "yyyyMMdd", null);
if (!String.IsNullOrWhiteSpace(tb_DateTo.Text))
dateTo = DateTime.ParseExact(tb_DateTo.Text, "yyyyMMdd", null);
if (dateFrom.HasValue)
journeys = journeys.Where(j => j.DateFrom >= dateFrom.Value);
if (dateTo.HasValue)
journeys = journeys.Where(j => j.DateTo <= dateTo.Value);
score:0
private DateTime getDate(string yyyyMmDd, DateTime defaultValue)
{
DateTime ret = DateTime.MinValue;
if (!DateTime.TryParse(yyyyMmDd, out ret))
return defaultValue;
return ret;
}
var to = DateTime.Parse(tb_DateTo.Text);
var from = DateTime.Parse(tb_DateFrom.Text);
journeys.Where(j=> getDate(j.DateFrom, DateTime.MaxValue) <= from && getDate(j.DateTo, DateTime.MinValue) >= to);
Source: stackoverflow.com
Related Articles
- How to select a date interval in IQueryable<>?
- How to select only the records with the highest date in LINQ
- Linq code to select one item
- How to Select Min and Max date values in Linq Query
- Entity Framework select one of each group by date
- Linq - Select Date from DateTime
- How can I select items with Linq by Date while ignoring Time portion of a DateTime property?
- Select only first record within a time interval
- select values tolist() when orderby distinct date
- Execute expression on another IQueryable source
- LINQ Source Code Available
- Linq select where date is yesterday
- .NET 4 Code Contracts: "requires unproven: source != null"
- c# Linq select distinct date time days
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- Create a Dynamic Linq to EF Expression to Select IQueryable into new class and assign properties
- Select doesn't work on IQueryable but does on IList
- MySQL entity framework Group and Select By Date
- LINQ VB how to select records with the max date (highest date)
- LINQ to select a column with max date from a group
- Select values with max date for each ID
- Unit testing code using IQueryable
- Linq select all numbers that are in any interval from another list of intervals
- LINQ to SQL: Select count rows for each date in a provided range?
- creating Linq to sqlite dbml from DbLinq source code
- Select Max Date items in group using Linq VB.NET
- Linq Select row Where date in Current month
- Avoiding duplicate code in Linq Select method
- Linq query how to select last one week data from today's date
- C# Linq IQueryable select flattened nested object list
- Linq Get count from multiple where condition
- LINQ and Devexpress Grid Datasource
- Parse certain data using JSON.Net
- Linq query using list or array of ids
- Specified Cast Invalid Byte to int
- Umbraco 7 LINQ to filter Model children on date range
- C# parsing xml document with LINQ where there are varying number of duplicate elements
- Check specific column in list for duplicates, skip null or empty values
- Convert IEnumerable<T> to List <T> Performance
- Problem updating multi-row
- LINQ Check If DateRange Contains Named Day or Check if DateRange Contains a Weekend
- Instantiate empty IQueryable for use with Linq to sql
- Linq query for below data
- Filter anonymous type collection
- FirstOrDefault() result of a struct collection?
- How to multiply and sum all numeric children properties in LINQ
- Populate list with missing dates LINQ
- Iteratin trought LINQ with index in collection
- Is it possible to order by column which is not present in groupby?
- Help with Linq expression to return a list of strings based on field count