2013-06-25 76 views
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 
} 
+1

有什麼問題? – 2013-06-25 05:41:11

回答

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