score:16
expression.constant takes in an object, so in your code the parameter is being boxed. boxing a nullable value actually boxes the underlying value (see boxing nullable types), so expression.constant thinks the parameter is a datetime instead of a datetime?.
you can force the constantexpression to be a datetime? by using the overload of expression.constant that takes in a type:
expression.constant(datetime.now, typeof(datetime?))
score:2
what it boils down to is that the nullable types are a rather leaky abstraction. they work fine when assigning a datetime to a datetime?, but with comparisons, things get tricky since system.nullable doesn't really know how to deal with them.
there is no implicit conversion between the datetime? and the datetime, so what you need to do is actually get a real datetime value out of the nullable to compare against. the easiest way to do that is to use the getvalueordefault()
method from the nullable datetime. this will give you the value if it is not null, and the default datetime (which is datetime.minvalue
, i believe) if it is null.
Source: stackoverflow.com
Related Query
- Linq DateTime expressions wont compare if only one is nullable
- how does one compare the date part of a nullable datetime in a linq / lambda query?
- Use Linq query to compare date only with DateTime field
- Compare only Date in nHibernate Linq on a DateTime value
- C# Linq compare only date from a datetime property
- Comparing Nullable Datetime got error Only parameterless constructors and initializers are supported in LINQ to Entities
- How to compare only Date portion in DateTime in LINQ to Entities (Case:- Dates are equal)
- Convert string[] to int[] in one line of code using LINQ
- Distinct in Linq based on only one field of the table
- How to compare only date components from DateTime in EF?
- Linq code to select one item
- Compare Two Lists Via One Property Using LINQ
- Linq Except considering only one property
- LINQ - 'The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.'
- Linq To Sql compare Time only
- Compare only time from datetime in entity framework 6 with odp.net Oracle 12c
- Linq remove only one item if there are duplicate
- Compare DateTime in EF Core Linq Query
- Why can't I cast nullable DateTime as string in a LinQ query?
- Select all columns but group by only one in linq
- How to compare minutes portion only of a TimeSpan in LINQ to Entities?
- How to pass LinQ Expressions from F# to C# code
- Linq call a function only once in one statement
- How to modify only one or two field(s) in LINQ projections?
- LINQ Source Code Available
- Linq to compare two lists of objects where one object has several lists
- LINQ to Entities DateTime Compare
- how do i use group by only on date from datetime in LINQ query
- How to compare only date part with linq expression?
- How can I combine this code into one or two LINQ queries?
More Query from same tag
- Linq Query Grouping Data table using two columns
- Entity Framework Many-To-Many + Count
- Linq: Nullable Object must have Value
- Linq casting in a Filter design
- Get the id of the latest record in group by with linq
- Splitting a list of dates/weeks into a and b weeks with LINQ
- convert code to use Linq
- How to check the attribute value string from linq in c#
- The type arguments for method cannot be inferred from the usage on Model
- Using an OrderBy after GroupBy in LINQ
- ListView.AddRange of custom Object using LINQ
- Which kind of join should i use? [EF4.0 code first ]
- Find duplicate in a list from a reference list
- Generic Type arguments as params
- LINQ filter condition
- Select single random item from list of items
- Include using Lambda expression
- Search, Add and update using LINQ
- Asp.Net MVC C# Cannot Implicitly Convert Type Issue
- Linq to SQL query filter with Lightswitch computed property
- Get list based on condition from another generic list
- Finding the difference between two DataTables
- linq select a random row
- Find a string in a list of strings with reflection
- Linq Acrobatics: How to flatten hierarical data models?
- LINQ's Query using fetch Dictionary object with Room Information
- To convert if condition to linq in cshtml
- Apply filters to a table based on conditions using LINQ and MVC C#
- EF custom select with entity property as parameters
- C# Create a Truncated Collection with LINQ