How to use properly the code below?
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
I need to return an IEnumerable of a particular object but I need to convert the collection object to a Queryable and convert the result again to List?
Below is my implementation, I don’t know if this is correct, I am using the code to unit test the generic repository.
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 = "rene_florendo2005@yahoo.com.ph",
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();
}
Any help or suggestions is highly appreciated.
Thank you.
If i’m not wrong :
Where(Expression<Func<T, bool>>)is an extension for a IQueryablebut
Where(Func<T, bool>>)is an extension for an IEnumerable.So try to change the parameter type of your function (or overload it), and you won’t need to cast to Queryable to use your predicate (don’t think that you need Expression)
EDIT
The other way would be to Compile your expression (var predicatefunc = predicate.Compile()), and use it in your “Where” clause. But I don’t think that’s the best way…