0
如何正確使用下面的代碼?如何使用此公共IEnumerable <T>查找(表達式<Func <T, bool>>謂詞)?
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
我需要返回一個特定對象的IEnumerable但我需要收集對象轉換爲可查詢並再次轉換結果列表?
下面是我的實現,我不知道這是否正確,我使用代碼來單元測試通用存儲庫。
public IEnumerable<tbl_SBAem_Attendee> Find(Expression<Func<tbl_SBAem_Attendee, bool>> predicate)
{
var listtblsbaemAttendee = new List<tbl_SBAem_Attendee>();
IEnumerable<tbl_SBAem_Attendee> enumerableAttendee=new List<tbl_SBAem_Attendee>();
for (var i = 0; i < 10; i++)
{
var tblSbAemAttendee = new tbl_SBAem_Attendee
{
AT_AID = i,
AT_Address = "Address1" + i,
AT_Address2 = "Address2" + i,
AT_City = "City",
AT_Email = "[email protected]",
AT_EventID = null,
AT_FirstName = "Rene" + i,
AT_LastName = "Florendo" + i,
AT_GID = null,
AT_Org = "Rene's Club",
AT_Phone = "888-8888",
AT_Phone2 = "999-8888",
AT_RegID = null,
AT_SendInfo = null,
AT_State = "NJ",
AT_Title = "Mr",
AT_Zip = "4102",
AT_date = null,
AT_opt1 = "option1",
AT_opt2 = "option2",
AT_opt3 = "option3",
AT_opt4 = "option4",
AT_opt5 = "option5",
AT_opt6 = "option6",
AT_userID = null,
AttendeeID = 1
};
listtblsbaemAttendee.Add(tblSbAemAttendee);
}
var result= listtblsbaemAttendee.AsQueryable().Where(predicate);
return result.ToList();
}
任何幫助或建議,高度讚賞。 謝謝。
謝謝。我會嘗試。 – user335160 2012-04-12 10:25:50
這是行得通的。但爲什麼大多數開發人員喜歡Expression>)而不是Func ? –
user335160
2012-04-12 10:39:11
「大多數開發人員」並不意味着很多...你當然看過用於db查詢的函數的例子(所以使用IQueryables,它可以避免在數據庫之前從數據庫中檢索數據),就是這樣。 – 2012-04-12 10:42:31