I understand that I can get a list of records from a specific table like this:
var artists = db.Artists.ToList();
However, I am attempting to create a Validation Factory for many of my Object Classes, I would like to send a string containing the Table Name (with Namespace), then call that table.
I've tried this: Dynamic table name in linq
I have the following in my DbContext:
public DbSet Set(string name)
{
// you may need to fill in the namespace of your context
return base.Set(Type.GetType(name));
}
Then I have this (the .Where
isn't working):
using (var db = new ApplicationDBContext())
{
// Since your DbSet isn't generic, you can can't use this:
// db.Set("Namespace.EntityName").AsQueryable().Where(a=> a.HasSomeValue...
// Your queries should also be string based.
// Use the System.Linq.Dynamic nuget package/namespace
var results = db.Set("x.Models.Music.Artist")
.AsQueryable()
.Where("Name != null") //Just an example
;
//Do something to validate
}
I get 'IQueryable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)
I'm sure it is a simple fix, but not sure how to resolve at the moment.