Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx]]

$
0
0

Hi All,

I getting above error:
Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxx]]

public static IQueryable<T> WhereAnd<T>(this IQueryable<T> Query, SearchCriteria[] Criterias)
{
if (Criterias.Count() == 0)
return Query;
LambdaExpression lambda;
Expression resultCondition = null;
// Create a member expression pointing to given column
ParameterExpression parameter = Expression.Parameter(Query.ElementType, "p");
foreach (var searchCriteria in Criterias)
{
if (string.IsNullOrEmpty(searchCriteria.Column))
continue;
MemberExpression memberAccess = null;
foreach (var property in searchCriteria.Column.Split('.'))
memberAccess = MemberExpression.Property
(memberAccess ?? (parameter as Expression), property);
ConstantExpression filter;
if (searchCriteria.Column.IndexOf("Date") > -1)
{
if (memberAccess.Type.Name == "Nullable`1")
{
Type t = typeof(DateTime?);
object searchVal = DateTime.Parse(searchCriteria.Value.ToString());
// Change the type of the parameter 'value'. it is necessary for comparisons (specially for booleans)
filter = Expression.Constant
(
Convert.ChangeType(searchVal, t)
);

At the Last 4 line above code.

Please help me.

Many thanks.

Regards,
Micheale


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>