Friday, 23 August 2013

WCF Data Services Client: How to select matching entities based on a provided T generic type and a Func?

WCF Data Services Client: How to select matching entities based on a
provided T generic type and a Func?

I'm using the WCF Data Services/Odata client library, and if I target the
Northwind sample OData store at
http://services.odata.org/Northwind/Northwind.svc/ , with the following
instruction I can retrieve the products which have a quantity bigger than
50:
var r = context.Products.Where(w=> w.UnitsInStock > 50)
My question is, suppose I want to have a generic class which must perform
this operation, but instead of hardcoding the entity to retrieve (Products
in this case) and the condition over the same entity (UnitsInStock > 50),
I would want to supply the entity object/name and the condition as a Func.
What I would like was to have something like the following class, where
the imaginary GetEntityType function would infere the entity from the
supplied T, but unfortunatelly GetEntityType does not exist and I haven't
found how to accomplish the same task:
public class OdataTesting<T>
{
...
public IQueryable<T> ReturnItem(Func<T, bool> selector)
{
return context.GetEntityType<T>().Where(w=> selector(w));
}
...
}
UPDATE:
I've found the CreateQuery method, which can be used as follows:
return context.CreateQuery<T>("Products").Where(w=> selector(w));
But...
1. I can't find a way to get the string entity name associated to a data
type. Is there a way to get it programatically from T?
2. The URI generator seems to have dificulties when functions are used to
filter the entities, so I'll have to investigate further for alternatives.
Sugestions are welcome

No comments:

Post a Comment