在LINQ动态表名 [英] Dynamic table name in linq

查看:205
本文介绍了在LINQ动态表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行使用动态表名的一些LINQ命令。例如,而不是

I'm trying to execute some LINQ commands using a dynamic table name. For example, instead of

var o = (from x in context.users select x);



我想使用类似

I want to use something like

var o = (from x in getTableObjectByName("users", context) select x);

更多或更少。下面的代码我到目前为止,这既编译和运行:

More or less. Here's the code I have so far, which both compiles and runs:

using (MySiteEntities ipe2 = new MySiteEntities()) {
    var propinfo1 = Type.GetType("MySiteNamespace.MySiteEntities").GetProperty("users");
    var propval1 = propinfo1.GetValue(ipe2, null);
}



运行,但总是返回零记录。 users表中最肯定包含的记录,并在当我把它直接使用第一种方法任何情况下,上述我得到的所有记录预期。 ?如何修改我的代码真正拉下来的记录,而不是只是一个空洞的集合

That runs, but always returns zero records. The users table most definitely contains records, and in any case when I call it directly using the first method above I get all of the records as expected. How can I modify my code to actually pull down records, rather than just an empty collection?

编辑:我也试过这样:

using (MySiteEntities ipe = new MySiteEntities())
{
    var prop = Type.GetType("MySiteNamespace.MySiteEntities").GetProperty("users");
    Type dbsetType = typeof(DbSet<>);
    dbsetType = dbsetType.MakeGenericType(Type.GetType("MySiteNamespace.user"));

    Type t = dbsetType.GetType();
    var val = prop.GetValue(ipe, null);
}

在这种情况下,代码不仅运行,但实际上返回的结果作为预期。然而, VAL 是一个对象。我需要将其转换为键入 DbSet<使用者> ,这将是很容易,但参数用户仅在运行时称为....剧组需要是动态的为好。我已经使用 Convert.ChangeType(VAL,t)的尝试;​​ ,但引发InvalidCastException(对象必须实现IConvertible)

In this case, the code not only runs, but actually returns the results as expected. However, val is an Object. I need to cast it to the type DbSet<user>, which would be easy enough, except that the parameter user is only known at runtime....the cast needs to be dynamic as well. I've tried using Convert.ChangeType(val, t);, but that throws an InvalidCastException (Object must implement IConvertible).

我怎么可以在 VAL 变量转换为一个实际可用的对象?

How can I convert the val variable to an actually usable object?

没有想法,如果这,是相关的,但是这是对的EntityFramework 4

No idea if this is relevant, but this is on EntityFramework 4.

推荐答案

在你的DbContext类,添加一个方法说叫集返回:

In your DbContext class, add a method say called Set that returns:

public DbSet Set(string name)
{
  // you may need to fill in the namespace of your context
  return base.Set(Type.GetType(name));
}



,您可以查询这样的:

Which you can query like this:

using (var db = new YourDataContext())
{
  // 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("Namespace.EntityName")
    .AsQueryable()
    .Where("SomeProperty > @1 and SomeThing < @2", aValue, anotherValue);
  // you can now iterate over the results collection of objects
}

在System.Linq.Dynamic的更多信息,可以发现的这里

More information on System.Linq.Dynamic can be found here

这篇关于在LINQ动态表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆