LEFT OUTER JOIN在LINQ [英] LEFT OUTER JOIN in LINQ

查看:124
本文介绍了LEFT OUTER JOIN在LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何执行左外连接在C#中的LINQ to对象,而无需使用加入上等号,到条款吗?有没有办法做到这一点与其中,条款?
正确的问题:
对于内部联接是容易的,我有这样的一个解决方案。

 列表< JoinPair> innerFinal =(从权利升的左派从r其中L.KEY == r.Key
                             选择新JoinPair {LeftId = l.Id,RightId = r.Id})

但对于左外连接我需要一个解决方案。我的是这样的事情,但它不工作

 名单,LT; JoinPair> leftFinal =(由L在权利从研发左派
                             选择新JoinPair {
                                            LeftId = l.Id,
                                            ?RightId =((L.KEY == r.Key)r.Id:0
                                        })

其中JoinPair是一个类:

 公共类JoinPair {长leftId;长rightId; }


解决方案

由于上注明:

101 LINQ示例 - LEFT OUTER JOIN

 变种Q =
    从C类
    加入产品P的c.Category等于p.Category为PS
    从ps.DefaultIfEmpty P()
    选择新{类别= C,产品名称= P == NULL? (无产品):p.ProductName};

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Correct problem: For inner join is easy and I have a solution like this

List<JoinPair> innerFinal = (from l in lefts from r in rights where l.Key == r.Key
                             select new JoinPair { LeftId = l.Id, RightId = r.Id})

but for left outer join I need a solution. Mine is something like this but it's not working

List< JoinPair> leftFinal = (from l in lefts from r in rights
                             select new JoinPair { 
                                            LeftId = l.Id, 
                                            RightId = ((l.Key==r.Key) ? r.Id : 0
                                        })

where JoinPair is a class:

public class JoinPair { long leftId; long rightId; }

解决方案

As stated on:

101 LINQ Samples - Left outer join

var q =
    from c in categories
    join p in products on c.Category equals p.Category into ps
    from p in ps.DefaultIfEmpty()
    select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };

这篇关于LEFT OUTER JOIN在LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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