score:-1
-you can use the timeofday property on the dates to compare them.
string timeasstring = "13:00";
from f in tablerows
where f.date.timeofday > datetime.parse("11-12-2012 "+timeasstring).timeofday
select f
edit
here is some code you can test that runs for me:
datetime d = datetime.parse("12-12-2012 13:00");
list<datetime> dates = new list<datetime>();
dates.add(datetime.parse("12-12-2012 13:54"));
dates.add(datetime.parse("12-12-2012 12:55"));
dates.add(datetime.parse("12-12-2012 11:34"));
dates.add(datetime.parse("12-12-2012 14:53"));
var result = (from f in dates where f.timeofday > d.timeofday select f);
edit 2
yea it seems that you needed to .tolist(), which, tbh you should have been able to figure out. i had no way of knowing what of collection you had. not sure either of us deserve a downvote for trying to help you when you don't supply an awful amount of information on the problem
score:-1
if you had a date
and a time
column in your table (as possible with sql server 2008), you could do this directly in sql.
as that's not the case, you have to do it like this:
// the first two lines can be omitted, if your bounds are already timespans
var start = startdate.timeofday;
var end = enddate.timeofday;
var filtereditems = context.items.tolist()
.where(x => x.datetimecolumn.timeofday >= start
&& x.datetimecolumn.timeofday <= end);
score:0
if you convert the date value to a double and use the fractional part of if, you get a number between 0 and 1 that represents a time in a day. having that, it is trivial to test for time intervals, where e.g. 13:45 would be 0,5729166667 (or more precise: 13:45.345 is 0,572920679).
you can do this because ef (i.e. 4.3, the version i use) translates cast and even math functions into sql:
mycontext.data.where(dt => dt.datetime.hasvalue)
.select(dt => dt.datetime.value).cast<double>()
.select(d => d - math.floor(d))
.where(... your comparisons
this translates to sql containing cast([date column] as float)
and sql's floor
function.
after your comments:
it looked so easy, but i can't find a way to instruct ef to do a cast
on a single property in a select()
. only the cast<>()
function is translated to cast
(sql), but that operates on a set.
well, fortunately, there is another way:
mycontext.data.where(dt => dt.datetime.hasvalue)
.select(dt => new
{
date = dbfunctions.truncatetime(dt.datetime.value),
ms = dbfunctions.diffmilliseconds(
dbfunctions.truncatetime(dt.datetime.value), dt.datetime.value)
})
.where(x => x.ms > lower && x.ms < upper)
where lower
and upper
are the totalmilliseconds
property of timespan
objects.
note that this is horribly inefficient in sql! the date functions are repeated for each comparison. so make sure that you do the comparisons on a set of data that has been confined as much as possible by other criteria. it may even be better to fetch data in memory first and then do the comparisons.
note: prior to ef6, dbfunctions
was entityfunctions
.
score:3
var filteredtimes = mycontext.mytable
.where(r => sqlfunctions.datepart("hour", r.datefield) >= 11 &&
sqlfunctions.datepart("hour", r.datefield) <= 13);
you need to include system.data.objects.sqlclient
to get sqlfunctions
.
Source: stackoverflow.com
Related Query
- Is possible to run a query with linq to search for a period of time?
- Entity Framework EF Query using LINQ for related Entities - Getting Customers with their orders from specific period of time only
- LINQ Query to search for users with partial name
- Does Linq search the entire database when I run a select statement with a time restricting where clauses?
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Search two lists for at least one match with LINQ
- Steps for a beginner to run very basic linq to sql query using Linqpad
- Linq query with multiple Contains/Any for RavenDB
- Using LINQ to search a byte array for all subarrays that start/stop with certain byte
- C# search query with linq
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Is it possible to use a string for a LINQ query expression?
- How can I create a dynamic LINQ query in C# with possible multiple group by clauses?
- linq query for tag system - search for multiple tags
- linq - how do you do a query for items in one query source that are not in another one?
- use query linq in search with empty field
- Create linq query to search for contact the same way smartphone will do so
- How to improve this LINQ query for search
- Is it possible to have fall-back for empty values in a LINQ query considering locale?
- Determine the source DataContext for a Linq to Sql query
- LINQ query for an aggregate count with a recursive relationship
- Query for existence of an object with LINQ (to Entities)
- Search for entire name based on three columns with LINQ
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- Which way is better Linq Select Query or For Loop to search something in generic List?
- linq query to split a time interval into daily batches with part days at start and end
- Stubbing Code for Test With Linq Expressions and Lambdas
- LINQ Query to Find the Maximum Mean for a Time Span
- Is It Possible to do this T-SQL Query with LINQ (to Entities)
- Linq sub query when using a repository pattern with EF code first
More Query from same tag
- can linq expression be case insensitive
- Need certain LINQ command using let
- LINQ Entity Framework Variable to select db Column
- Get a sequence of multiples of an integer between two values using LINQ
- C# get array from dictionary with respect to other array
- I have a linq List Select issue
- How to do condition statement in LINQ based on code variables?
- How do i use navigation properties correctly EF?
- C# Linq Average
- LINQ check string property for NULL in generating a ToList()
- Why does this bulk insert not work as expected?
- Extension Method Limiting The Number Of Linq Results?
- How can i get the Parent Categories with C#? (Dot Net Fiddle included)
- How to insert a record with LINQ and C# and return the Primary Key of that record
- Linq Group By Multiple Columns
- Get Entity From Table Using Reflection From Abstract Type
- Get all column names of a DataTable into string array using (LINQ/Predicate)
- Convert SQL query that uses CAST calls to LINQ
- Why doesn't a null cause a NullReferenceException when mapping to and from a null type?
- Help with Linq query, need to set ID based on results of a sub-query
- Entity Framework Core GroupBy - aggregate functions on related entities
- Get all the keys in Array of Dictionary
- Using LINQ + XML to scrape website
- C# Linq Lamba elements count
- Is it possible to do XQuery through linq?
- C# Linq ANY vs ALL - Performance
- Using IN subquery in Entity Framework using Linq
- How to call a stored procedure that returns dynamic columns in Entity Framework?
- How to Select All Fields in Linq to Entity Using Lambda Expression?
- Query composition over LINQ Entity-Framework