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: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);
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);
Source: stackoverflow.com
Related Query
- How to select a date interval in IQueryable<>?
- How to select only the records with the highest date in LINQ
- How to Select Min and Max date values in Linq Query
- How can I select items with Linq by Date while ignoring Time portion of a DateTime property?
- LINQ VB how to select records with the max date (highest date)
- Linq query how to select last one week data from today's date
- How to clear default select clause on IQueryable resultset
- How to search date in a dynamic query using IQueryable in c#
- How to select the max date from table in c# linq query
- How to Select top (5) contributors group by Business type code in c# linq query
- ASP.net Getting Max Date of database Date column How to avoid Null in Date Column C# My code Attached
- how to select the latest date from a IQueryable.GroupBy?
- How to write aggregate query in LINQ reusing part of the select code
- How to group a class by date and select Max(Date), primary-key and foreign-key?
- How to select a record and only it's children with the highest date in LINQ to SQL
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- How can I format a date in the Select property of an EntityDataSource
- How to write C# LINQ code to select based on condition
- How do I resolve an error on Union within IQueryable Select statement
- How to select a column based on date condition in LINQ query
- How to convert a string to C# code in the SELECT of C# LINQ
- LinqDataSource: How to assign IQueryable value to where parameters in code
- LINQ how to select the newest folder according to creation date
- How to select records of user's history of items, where the date is latest for the items in LINQ to entities lambda?
- How to select entire row by distinct column MVC code first
- How to use LINQ to select object with minimum or maximum property value
- How to compare only date components from DateTime in EF?
- Linq code to select one item
- How can I do SELECT UNIQUE with LINQ?
- How do I find the text within a div in the source of a web page using C#
More Query from same tag
- Linq OrderBy against specific values
- XML LINQ Assistance
- If statement and assignments in lambda expressions
- Case Statements in LINQ query - AnonymousType to String?
- ASP.net 4.0 Entity Data Model Mysql Not Treating Mysql Enums Right
- Getting Total Record by Group by in Linq
- Linq Query for TOP 11-20
- Doing Recursive Function using LINQ and Delegates
- I need to extract string(that is coming from a list) from line
- select object which matches with my condition using linq
- Count the number of Files modified in the last hour using linq
- How to make generic lambda by MakeGenericType
- Why does this Linq query return 0 for Count()?
- LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- LINQ with ObservableCollection
- C# Linq union multiple properties to one list
- Counting With C# Using Linq Or NOT
- Inserting Related Records into Database via Entity Framework
- More than one record is updated EF-Linq C#
- How can I restrict a LINQ Query?
- C# How do i return iGrouping to a list using lambda expression
- How to use IN Operator inside Where Condition in Linq
- Are there any performance difference between these 2 linq queries, are they identical or essentially different?
- Is LINQ Join operator using Nested Loop, Merge, or HashSet Joins?
- Check if XElement with particular value exist
- How to get first level of children by LINQ
- Iterating result of Linq query
- Return LINQ Query and INSERT Data
- Linq SubQuery for Unrelated entity variable
- Customize DataGridView filled by linq-query