本文介绍了LINQ中的左外连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用 join-on-equals-into
子句的情况下在 C# LINQ 中对对象执行左外连接?有没有办法用 where
子句做到这一点?正确问题:对于内部连接很容易,我有这样的解决方案
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
})
其中 JoinPair
是一个类:
public class JoinPair { long leftId; long rightId; }
推荐答案
如上所述:
101 LINQ 示例 - 左外连接
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 };
这篇关于LINQ中的左外连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!