0

我有一个基于模板 T 加载的数据库。但是,现在我想根据字符串加入其他表或传入“T2”模板。

如何创建这样的函数来生成 IQueryable?

public void createJoinedTable<T, T2>(T2 join_table, string join_on_this_property, string order, string order_by)
            where T : class
            where T2 : class
   {

            var table = GetGenericTable<T>(); // I have the IQueryable<T> of the main table. 
            // now join the joined table.
            var id = 1;
            table = table    // your starting point - table in the "from" statement
               .Join(join_table, // the source table of the inner join
                  firsttable => post.myid,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
                  secondtable => meta.othertableid,   // Select the foreign key (the second part of the "on" clause)
                  (firsttable, secondtable) => new { Unknown = firsttable , Unknown2 = secondtable}) // selection
               .Where(x => x.Unknown.ID == id);    // where statement

           table = table.CustomOrderByDescending(order_by, direction); // custom ordering by string
           m_queryable = table; // record results.
   }

问题是,我不能做 .Join() 因为它不受 Entity 类的约束。它被限制为一个通用的“类”。

where T : class 而不是 where T: MyEntityTable

好吧,如果我在参数中这样做了,那么拥有“通用连接表函数”有什么意义呢?

我希望能够根据基于文本的参数加入任何我想要的内容。

我将如何使用“join_on_this_property”来帮助我完成此任务?

奖励挑战:基于“列出表,列出 join_ON_properties”加入无限数量的表——但这可能非常复杂。

4

0 回答 0