So, basically I am trying to get a DbSet
returned from entity name (string). This is how far I have gotten:
var customers = db.Set(Type.GetType("App.Model.Customer,
App.Model,
Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null"));
But then I cannot perform other LINQ like Any()
or Single()
. Why is that and how can I change it to do so?
The non-generic overload of
DbContext.Set
returns an equally non-genericDbSet
. One of the interfaces this class implements isIQueryable
, again, non generic. That's the reason why it can't serve as input to any of these LINQ extension method that are defined on the generic typeIQueryable<T>
.So in fact, if you still want to use LINQ, you only postpone the moment when you have to convert to a generic
IQueryable
. You could do......but then of course you lose the whole point of defining the type dynamically, at runtime.
Once you start dynamically, you have to continue dynamically. One way to do that is by adding
System.Linq.Dynamic
to your project. Then you can write queries like......which will return you the
Customer
(wrapped in anIQueryable<Customer>
) havingCustomerId == 1
.You can even use the
Find
method:This will return a single
Customer
instance.With the requirements you have, no, if the requirements were to change you might be able to work around it. If you get your type as string at runtime, how do you believe you could possibly program against it before you even compile the code?
Static typing need static types to work, you can do some ninja
dynamics
with interfaces to get around part of it, but your question provides no such information.Try casting the non-generic IQueryable to a generic (where the generic property is a super class or even just object), e.g.:
This worked for me!