kriper
kriper

Reputation: 1054

How to write a generic where for several types in linq to entities?

I have a lot of classes

public class City
{
    public int CityID { get; set; }
    public string Name { get; set; }
    public string Comment { get; set; }
    public virtual ICollection<ApplicationUser> DryCleanings { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateChanged { get; set; }
    public bool IsDeleted { get; set; }
}

    public class Marker
{
    public int MarkerID { get; set; }
    public string Name { get; set; }
    public string Comment { get; set; }
    public byte[] Icon { get; set; }
    public virtual IEnumerable<ApplicationUser> Cleanings { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateChanged { get; set; }
    public bool IsDeleted { get; set; }
}
.
.
.

They all have the same three properties

    public DateTime DateCreated { get; set; }
    public DateTime DateChanged { get; set; }
    public bool IsDeleted { get; set; }

I need to write same where query for each of them, something like this

_db.Cities.Where(c => !c.IsDeleted && c.DateChanged > oldDate && c.DateChanged < oldDate);
_db.Markers.Where(m => !m.IsDeleted && m.DateChanged > oldDate && m.DateChanged < oldDate);

i do not want write this query for every class. Can i write this predicate once and use for all?

ps. I tried inherited from Interface

    public interface IDate
{
    public DateTime DateCreated { get; set; }
    public DateTime DateChanged { get; set; }
    public bool IsDeleted { get; set; }
}

and write something like this

 Expression<Func<IDate, bool>> lessOldDate = c => !c.IsDeleted && c.DateChanged > oldDate && c.DateChanged < oldDate;

but i getting type IDate however i need Marker

enter image description here

Upvotes: 1

Views: 69

Answers (3)

Jean Hominal
Jean Hominal

Reputation: 16796

You can resolve this issue in a less forced way (than using Enumerable.Cast or Enumerable.OfType) by defining a generic method:

public static IQueryable<T> WhereLessOld<T>(this IQueryable<T> source, DateTime oldDate)
    where T : IDate
{
    return source.Where(c => !c.IsDeleted && c.DateChanged > oldDate && c.DateChanged < oldDate);
}

Note: There may be an issue with the code above if the LINQ provider that you are using does not handle interfaces correctly - in that case, you can use the following code:

using System;
using System.Linq;
using System.Linq.Expressions;

public static IQueryable<T> WhereLessOld<T>(this IQueryable<T> source, DateTime oldDate)
    where T : IDate
{
    var param = Expression.Parameter(typeof(T));
    var filterExpression =
        Expression.And(
            Expression.Not(Expression.Property(param, "IsDeleted")),
            Expression.And(
                Expression.GreaterThan(
                    Expression.Property(param, "DateChanged"),
                    Expression.Constant(oldDate)
                ),
                Expression.LessThan(
                    Expression.Property(param, "DateChanged"),
                    Expression.Constant(oldDate)
                )
            )
        );
    var delegateExpression = Expression.Lambda<Func<T, bool>>(filterExpression, param);

    return source.Where(delegateExpression);
}

And then you can write:

var testQuery = db.Markers.WhereLessOld(oldDate);

Upvotes: 3

Carles Company
Carles Company

Reputation: 7216

Use the Cast method:

var testQuery=db.Markers.Where(lessOldDate).Cast<Marker>();

also see this related question.

Upvotes: 1

Jamiec
Jamiec

Reputation: 136239

Use Enumerable.OfType

var testQuery = db.Markers.Where(lessOldDate).OfType<Marker>().ToList();

Upvotes: 2

Related Questions