I am using auto generated OData Client, and I am trying to create a generic client class.
I want to get by some predicates.
public T Get<T>(Func<T, bool> predicate)
{
var entityName = GetEntitySetName<T>();
var query = _d365Context.CreateQuery<T>(entityName);
return query.Where(predicate).FirstOrDefault();
}
private static string GetEntitySetName<T>()
{
var originalEntitySetName = typeof(T).GetCustomAttributes(typeof(EntitySetAttribute), true).Cast<EntitySetAttribute>().SingleOrDefault()?.EntitySet;
return originalEntitySetName;
}
this code is reading all data and not applying the predicates. What I have already tried.
- I know query has AddQueryOption but I dont want to use it.
- I can also use context.EntityName, but it will be hardcoding of entities.
So my question is, is it possible to achieve what I am trying to do? is there any existing library available?
PS - I dont want to use Simple.Odata.Client
One idea is flowing in my mind, to use predicate and parse it somehow and use it in AddQueryOption (this method takes string name and object value). but I don't have much experience with Func, so not sure if it can be done.