Search code examples
asp.net-mvcentity-frameworklinqkit

How to pass a db object and model object to function


I want to turn this into a function. I will be passing the db object and the MVC model object. Here is the original code:

var SomethingApproval = db.Somethings.AsQueryable();
var predicate = PredicateBuilder.True<Something>();
predicate = predicate.And(p => p.Approved == "N");
SomethingApproval = SomethingApproval.AsExpandable().Where(predicate);

I want a function like this

Function ApprovedItems (dbObject, modelObject)
{

var SomethingApproval = dbobject.AsQueryable();
var predicate = PredicateBuilder.True<modelObject>();
predicate = predicate.And(p => p.Approved == "N");
SomethingApproval = SomethingApproval.AsExpandable().Where(predicate);
return SomethingApproval
}

Solution

  • Going out on a limb here... think its a generics question

    public IQueryable<T> ApprovedItems<T>(DbObject db)
        where T : IApprovable
    {
        var query = db.Set<T>();
        return query.Where(p => p.Approved == "N");
    }
    
    public interface IApprovable
    {
        //By the way I do not Approve of an 
        //approve flag with type of string
        string Approved {get;}
    }