如何加入用流利的LINQ or子句 [英] How to join with an or clause in Fluent LINQ

查看:109
本文介绍了如何加入用流利的LINQ or子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接两个表

该TSQL是:

SELECT * 
FROM User u INNER JOIN Hierarchy h ON u.OrganisationId = h.OrganisationId 
   OR u.OrganisationId = h.OwnerOrganisationId

我已经搜查,没有人用流利的这个答案。我能想到的最接近的是这样的:

I have searched and no one has an answer for this with fluent. The closest I can think of is this:

var join1 = context.User.Join(context.Hierarchy, u => u.OrganisationId, h => h.OrganisationId, uh => new {u, h});
var join2 = context.User.Join(context.Hierarchy, u => u.OrganisationId, h => h.OwnerOrganisationId, uh => new {u, h});
var desiredResult = join1.Union(join2);

这似乎是它可以是非常低效虽然。

This seems like it could be highly inefficient though.

推荐答案

流利的语法

var orJoin = context.User.SelectMany(
                u => context.Hierarchy.Where(h => u.OrganisationId == h.OrganisationId || u.OrganisationId == h.OwnerOrganisationId),
                (u, h) => new { u, h }
             );






查询语法

var orJoin = from u in context.User
             from h in context.Hierarchy
             where u.OrganisationId == h.OrganisationId || u.OrganisationId == h.OwnerOrganisationId
             select new { u, h };

这篇关于如何加入用流利的LINQ or子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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