c# - Func parameter in LINQ expression -
i have problem func delegate in linq expression. problematic part of method (repository.items iqueryable<t>) :
public static actionresult xxx<t>(irepository<t> repository, func<t, int> keyextractor, int id = 0) { if (id == 0) return ... t item = repository.items.where(x => keyextractor(x) == id). firstordefault(); if (item == null) return ... try { repository.deleteitem(item); return ... } catch (exception e) { return ... } } but when run method, error type of node not supported in linq entities. tried version predikate had no luck @ all.
any ideas how fix it?
i find out 1 possible way. linq performs delayed execution must first enforce execution this:
t item = repository.items.asenumerable().where(x => keyextractor(x) == id)
if want work on iqueryable<t>, argument must expression<func<t, int>>, not func<t, int>.
func<t, int> work ienumerable<t>.
where extensions methods has same name iqueryable<t> , ienumerable<t>, not same arguments...
(by way write repository.items.firstordefault(x => keyextractor(x) == id);
Comments
Post a Comment