实体框架Lambda表达式中的交叉联接 [英] Cross Join in Entity framework Lambda Expressions

查看:61
本文介绍了实体框架Lambda表达式中的交叉联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在EF中加入以下内容?表之间没有关系,没有外键.

How can I do the following join in EF? Tables have no relation with each other, no foreign keys.

Select t1.ID,  t1.firstname, t2.ID,t2.name from MY_TEST_TABLE1 t1, MY_TEST_TABLE2 t2
where t1.firstname = t2.name

推荐答案

您可以执行以下操作:

var query= from t1 in context.MY_TEST_TABLE1
           from t2 in context.MY_TEST_TABLE2
           where t1.firstname == t2.name
           select new { Table1Id= t1.ID, FirstName= t1.firstname, Table2Id=t2.ID,Name= t2.name};

在Linq to Entities中进行交叉联接的另一种方法是使用 SelectMany 扩展方法:

Another way to do a cross join in Linq to Entities is using SelectMany extension method:

var query= context.MY_TEST_TABLE1.SelectMany(
    t1=>context.MY_TEST_TABLE2
        .Where(t2=>t1.firstname == t2.name)
        .Select(t2=>new { 
            Table1Id= t1.ID,
            FirstName= t1.firstname, 
            Table2Id=t2.ID,
            Name= t2.name
        })
    );

这篇关于实体框架Lambda表达式中的交叉联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆