0
我想通過使用lambda的匿名屬性到一個通用函數並在那裏訪問它。
以及如何訪問裏面的屬性。如何通過lambda將匿名屬性傳遞給泛型函數?
using (CommentsRepository commentsRepository = new CommentsRepository())
{
var comments = commentsRepository.GetAllComments();
Foo<Comment>(comments, 0,com=>com.ID); //Comment is an EF entity
}
public static void Foo<TObject>(IEnumerable<TObject> list, int iCurID, <Func<TObject, TProperty> property) where TObject : class
{
foreach (var cat in list.Where(/*How to access the property*/==iCurID)
{
int x = cat.property;
}
}
什麼是propertySelector?你的意思是財產(x)? – urker 2011-04-02 11:39:11
@urker:對不起,我打算改變參數的名稱 - 我個人也會改變「iCurID」,這也是合理無意義的。 – 2011-04-02 11:51:02
@Jon Skeet,另一個問題:請參閱更新 – urker 2011-04-02 13:22:13