linq 中的动态表名 [英] Dynamic table name in linq

查看:58
本文介绍了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);

我想使用类似的东西:

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);
}

运行,但总是返回零记录.用户表绝对包含记录,无论如何,当我使用上面的第一种方法直接调用它时,我会按预期获得所有记录.如何修改我的代码以实际下拉记录,而不仅仅是一个空集合?

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 类型,这很容易,除了参数 user 仅在运行时才知道......转换需要也是动态的.我试过使用 Convert.ChangeType(val, t);,但这会抛出一个

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(对象必须实现 IConvertible).

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 类中,添加一个名为 Set 的方法,该方法返回:

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));
}

您可以这样查询:

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天全站免登陆