简体   繁体   English

C#Linq对模型使用变量

[英]C# Linq using variable for Model

Trying to write a piece of code that is dynamic in that it can accept any number of possible model definitions. 尝试编写动态代码,使其可以接受任何数量的可能的模型定义。

Current hard code is: 当前的硬代码是:

var items = _context.Events.ToList();

foreach (var item in items)
{
     (...)
}

What I would like to do is to make the _context.Events.ToList(); 我想做的是使_context.Events.ToList(); be more like _context.{variable that holds model name}.ToList(); 更像是_context.{variable that holds model name}.ToList();

Something like: 就像是:

var modelName = "Table1"
var items = _context.modelName.ToList();

foreach (var item in items)
{
     (...)
}

I thought about declaring items as a generic variable, that way it was available to the entire method even if set inside an if or switch, but no idea on what to declare it as. 我考虑过将项目声明为通用变量,这样,即使在if或switch中设置它,整个方法也可以使用它,但对于将其声明为什么一无所知。

Is something like this possible? 这样的事情可能吗?

Try this : 尝试这个 :

var table = (ITable)context.GetType()
                           .GetProperty(modelName)
                           .GetValue(context, null);

I hope be helpful :) 希望对您有所帮助:)

Entity Framework has a generic Set<TEntity> accessor, but the type must be known at compile-time: 实体框架具有通用的Set<TEntity>访问器,但是必须在编译时知道类型:

var foo = _context.Set<Foo>();

If you have the type as a string variable, though, your options are extremely limited. 但是,如果将类型作为字符串变量,则您的选择将非常有限。 You can technically use reflection to get at the right DbSet , but you're going to lose the generic IQueryable<TEntity> interface, and you'll be stuck with the much more limited IQueryable interface, and by "much more limited", I mean you basically can't do anything but materialize the set. 从技术上讲,您可以使用反射来获得正确的DbSet ,但是您将失去通用的IQueryable<TEntity>接口,并且将使用更加受限制的IQueryable接口,而由于“受更多限制”,我意味着除了具体化设置,您基本上什么也不能做。

If you want to type it via a string variable, but still have at least some querying ability, you'll need to employ a base class that's shared between all the entity types you'd want to use in this way. 如果要通过字符串变量键入它,但至少仍具有一定的查询能力,则需要使用一个基类,该基类在您要以此方式使用的所有实体类型之间共享。 For example, if you have different "event" types, and you can make them all inherit from Event , then you could do something like: 例如,如果您具有不同的“事件”类型,并且可以使它们全部继承自Event ,则可以执行以下操作:

MethodInfo method = typeof(Queryable).GetMethod("OfType");
MethodInfo generic = method.MakeGenericMethod(new Type[] { eventType });
var set = (IQueryable<Event>)generic.Invoke(null, new object[] { _context.Events });

If your eventType was "Film", for example, that would effectively give you the same queryset as something like _context.Set<Film>() (where Film would be a derived class of Event ). 例如,如果您的eventType是“电影”,那将有效地为您提供与_context.Set<Film>()类的相同_context.Set<Film>() (其中FilmEvent的派生类)。 You could then utilize your normal LINQ query functionality like Where , Select , etc. on set . 然后,您可以在set上利用常规的LINQ查询功能,例如WhereSelect等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM