0

我想把它变成一个函数。我将传递 db 对象和 MVC 模型对象。这是原始代码:

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

我想要这样的功能

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
}
4

1 回答 1

1

在这里冒险......认为这是一个泛型问题

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;}
}
于 2013-06-25T07:54:19.667 回答