简体   繁体   English

如何使用Entity Framework 6按行号进行内部联接

[英]How to do an inner join by row number using Entity Framework 6

Two tables: 两张桌子:

Table A has following data: 表A具有以下数据:

Type1 A
Type1 B
Type1 C
Type1 D

Table B has following data: 表B具有以下数据:

Type2 X
Type2 Y
Type2 U
Type2 V

I want to get the following output: 我想得到以下输出:

new { A, X }
new { B, Y }
new { C, U }
new { D, V }

Is this possible using Entity Framework? 使用实体框架可以做到吗? The problem I am facing now is there're 8 different entity tables, each represent a type. 我现在面临的问题是有8个不同的实体表,每个实体表代表一种类型。 The app has an index page, we want to show 6 items of each table on the index page. 该应用程序有一个索引页面,我们希望在索引页面上显示每个表的6个项目。 We can do this one by one of course, but that will hit the DB 8 times, which may not be very efficient. 我们当然可以一个接一个地执行此操作,但这将使数据库达到8次,这可能不是很有效。 What's the right way to solve such a problem using EF 6? 使用EF 6解决此类问题的正确方法是什么?

Try this one: 试试这个:

var AX = db.tableA 
.Join( 
tableB, 
foo1 => foo1.A, 
foo2 => foo2.X, 
(foo1, foo2) => new { foo1, foo2}
.ToList()



var BY = db.tableA 
    .Join( 
    tableB, 
    foo1 => foo1.B, 
    foo2 => foo2.Y, 
    (foo1, foo2) => new { foo1, foo2}
    .ToList()

  var CU = db.tableA 
    .Join( 
    tableB, 
    foo1 => foo1.C, 
    foo2 => foo2.U, 
    (foo1, foo2) => new { foo1, foo2}
    .ToList()

  var DV = db.tableA 
    .Join( 
    tableB, 
    foo1 => foo1.D, 
    foo2 => foo2.V, 
    (foo1, foo2) => new { foo1, foo2}
    .ToList()

If you use EF , probably all your tables have int PK with identity. 如果使用EF ,则可能所有表都具有标识的int PK If you also have never deleted separated rows, located between others, from these tables, you can use Id columns for join condition. 如果您也从未从这些表中删除位于彼此之间的分隔行,则可以使用Id列作为连接条件。 You can simple extend this example to desired number of tables with same manner. 您可以使用相同的方法将示例简单地扩展到所需的表数。

var tableA = from itemA in ctx.tableA
             let minId = ctx.tableA.Min(x => x.Id)
             select new { itemA, index = itemA.Id - minId };

var tableB = from itemB in ctx.tableB 
             let minId = ctx.tableB.Min(x => x.Id)
             select new { itemB, index = itemB.Id - minId };

var answer = (from itemAA in tableA
              join itemBB in tableB on itemAA.index equals itemBB.index
              select new { itemAA.itemA, itemBB.itemB}).ToList().SelectMany(x => {
                  var result = new List<Tuple<Type1, Type2>>();
                  var props1 = x.itemA.GetType().GetProperties().ToList();
                  var props2 = x.itemB.GetType().GetProperties().ToList();
                  for(var i = 0; i < Math.Min(props1.Count, props2.Count); i++)                              
                       result.Add(new Tuple<Type1, Type2>((Type1)props1[i].GetValue(x.itemA), (Type2)props2[i].GetValue(x.itemB)));
                   return result;                                  
              }).ToList();

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

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