简体   繁体   中英

cast Table<T> to something

I have a datacontext, and it has Authors table.

public partial Author:IProductTag{}

I want to cast Table<Authors> object to Table<IProductTag> , but that appears to be impossible. 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. I have few other tables, entities of which implement 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. 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. 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).

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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