vendredi 21 avril 2017

Entity Framework: Apply filter to a queryable using a propery of known name

I want to use a property of an entity model to filter the result;

This is the code I would normally use:

db.Users.Where(ent => ent.Id > 5).ToList();

But I can only access the property by its name as string ("Id") or by its ProperyInfo.

This is what I tried to use but it doesn't seem to work:

db.Users.Where(ent => (int) ent.GetType().GetProperty("Id").GetValue(ent,null) > 5).ToList();

Note that the where clause might get more complex, and I might use another property type (not int).





Aucun commentaire:

Enregistrer un commentaire