I am having a go at creating a generic xmlRepository where I can pass in any type and get a list back.
At the moment my code looks like this (Only way I can get it to work):
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
var filename = GetEntityFileName<TEntity>();
var doc = XDocument.Load(filename);
var query = (from p in doc.Descendants(entityName)
select (ServiceAccount)p).AsQueryable().Cast<TEntity>();
return query;
}
I would like to swap out the
select (ServiceAccount)p).AsQueryable().Cast<TEntity>();
with
select (TEntity)p).AsQueryable();
TEntity is the same object as ServiceAccount,
Is this possible with LINQ to XML?
I have a SQLRepository, using EF, which has the same method in it and I am looking for the LINQ to XML equivalent of
((IObjectContextAdapter)_context).ObjectContext.CreateQuery<TEntity>(entityName);
If indeed there is one.
You need to specify how you’re going to convert an
XElementto an instance ofT. Currently your code simply won’t work unlessServiceAccountalready has an explicit conversion fromXElement.You may want to have a convention of having a static
FromXElementmethod in each entity type – you could then invoke that from reflection, or even have a dictionary:You’d then write something like:
It’s not really clear why you’re using
IQueryable<T>here, by the way… anything wrong with justIEnumerable<T>?