My question is related to this one: linq-expression-with-generic-class-properties
This time I would like to get newly created objects which have the id in common. The id is in fact the foreign key and therefore can have different names.
I came up with the following:
public static IEnumerable<T> GetNew<TId, T>(IQueryable<T> objects, TId id, DateTime date, Expression<Func<T, TId>> idSelector)
where T : class, ISyncable<TId>
{
return objects.Where(o => idSelector(o) == id && o.CreatedDate > date);
}
The method would be called like this:
var modified = GetNew(dbObjects, id, date, entity => entity.PersonId);
Unfortunately I get the error:
'idSelector' is a variable but is used like a method.
The expression passed to the method should do something like that:
objects.Where(o => o.PersonId == id && o.CreatedDate > date);
You need to build a new expression representing the condition and pass that to
Where
:EDIT: If you know that the
idSelector
is a property expression, you could extract the referenced property and create a new property expression instead of usingInvoke
: