简体   繁体   English

将表<T>转换为某种东西

[英]cast Table<T> to something

I have a datacontext, and it has Authors table. 我有一个datacontext,它有Authors表。

public partial Author:IProductTag{}

I want to cast Table<Authors> object to Table<IProductTag> , but that appears to be impossible. 我想将Table<Authors>对象转换为Table<IProductTag> ,但这似乎是不可能的。 I am trying to do that because I want my method to be able to work with different tables which come as input parameters. 我试图这样做,因为我希望我的方法能够使用不同的表作为输入参数。 To be more specific, I need to execute OrderBy and Select methods of the table. 更具体地说,我需要执行表的OrderBy和Select方法。 I have few other tables, entities of which implement IProductTag . 我有很少的其他表,其实体实现IProductTag。 Also, I tried to write a function like: 另外,我试着写一个像这样的函数:

public static void MyF<t>(){ 
Table<t> t0 = (Table<t>)DataContext.GetMyTableUsingReflection(); 
}

But it fails at compile-time. 但它在编译时失败了。 And if I cast the table to something like ITable or IQueriable, then the OrderBy and Select functions simply don't work. 如果我将表格转换为类似ITable或IQueriable的东西,则OrderBy和Select函数根本不起作用。 So how do you deal with it? 那你怎么处理它?

I suspect you want to make your method generic - so instead of 我怀疑你想让你的方法通用 - 所以不要

void DoSomethingWithTable(Table<IProductTag> table)

you should have 你应该有

void DoSomethingWithTable<T>(Table<T> table) where T : class, IProductTag

That should work fine, assuming you only need to read entities (and apply query operators etc). 这应该工作正常,假设您只需要读取实体(并应用查询运算符等)。 If that doesn't work for you, please give more details about what your method needs to do. 如果为你工作,请介绍你的方法需要做更多的细节。

(You say that your attempt to use reflection failed, but you haven't said in what way it failed. Could you give more details?) (你说你尝试使用反射失败了,但你没有说它以什么方式失败。你能提供更多细节吗?)

I have no idea what a ProductTag is so I've used different types to show my solution to this problem. 我不知道ProductTag是什么,所以我用不同的类型来展示我对这个问题的解决方案。 Yes there doesn't seem to be a way to get a Table<T> , but you can get IQueryable<T> which works just as well (at least for my situation). 是的,似乎没有办法获得一个Table<T> ,但你可以得到IQueryable<T> ,它也可以(至少在我的情况下)。

I have a simple analytics database, where each website has its own table containing both generic and specific items. 我有一个简单的分析数据库,每个网站都有自己的表,包含通用和特定项目。 I wanted to use an interface for the shared data. 我想使用共享数据的接口。

public interface ISession 
{
    public DateTime CreateDt {get; set; }
    public string HostAddress {get; set; }
    public int SessionDuration {get; set; }
}

public static IQueryable<ISession> GetQueryableTable(MyDataContext db, string site)
{
     Type itemType;

     switch (item) 
     {
        case "stackoverflow.com":
            itemType = typeof(Analytics_StackOverflow);
            break;

        case "serverfault.com":
            itemType = typeof(Analytics_ServerFault);
            break;

        default: throw Exception();
     }

     return db.GetTable(itemType).Cast<ISession>();
}

You can then do a query like this : 然后,您可以执行以下查询:

  var table = GetQueryableTable(db, "stackoverflow.com");
  var mySessions = table.Where(s => s.HostAddress == MY_IP);

To create a new row you can use reflection : 要创建新行,您可以使用反射:

  var rowType = typeof(Analytics_ServerFault); 
  var newRow =  (ISession) rowType.GetConstructor(new Type[0]).Invoke(new object[0]);

(I have a function to get GetRowType - which is not shown here). (我有一个获取GetRowType的函数 - 这里没有显示)。

Then to insert into the table I have a separate helper function: 然后插入表中我有一个单独的辅助函数:

 public static void Insert(MyDataContext db, ISession item) 
 {
     // GetTable is defined by Linq2Sql
     db.GetTable(GetRowType(domain)).InsertOnSubmit(item);
 }

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

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