Reputation: 1020
Stuck with error:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
For a simple example, let's try to get an object from database using entity framework without actually knowing its type:
private DbContext db;
private dynamic test(dynamic entity)
{
return db.Set(entity.GetType()).First(x => x.Id == entity.Id);
}
How can I make this work?
Upvotes: 0
Views: 10806
Reputation: 203829
Use generics to do this, rather than dynamic
:
private DbContext db;
private T test<T>(T entity)
where T : BaseEntity
{
return db.Set<T>().First(x => x.Id == entity.Id);
}
Have a BaseEntity
that has an Id
property, to ensure that the given entity has an Id.
Upvotes: 4