gpt4 book ai didi

c# - linq中的左外连接

转载 作者:行者123 更新时间:2023-11-30 13:24:32 25 4
gpt4 key购买 nike

我有以下查询,但我不知道如何在表 1 上进行左外连接。

var query = (from r in table1
join f in table2
on r.ID equals f.ID
select new
{
r.ID,
r.FirstName,
r.LastName,
FirstNameOnRecord =
(f != null ? f.FirstName : string.Empty),
LastNameOnRecord =
(f != null ? f.LastName : string.Empty),
NameChanged =
(f != null
? (f.FirstName.CompareTo(r.FirstName) == 0
&& f.LastName.CompareTo(r.LastName) == 0)
: false)
}).ToList();

最佳答案

引用thisthis了解更多信息的示例和您的案例将是这样的-

var query = from r in table1
join f in table2
on r.ID equals f.ID into g
from f in g.DefaultIfEmpty()
select new
{
r.ID
, r.FirstName
, r.LastName
, FirstNameOnRecord = (f != null ? f.FirstName : string.Empty)
, LastNameOnRecord = (f != null ? f.LastName : string.Empty)
, NameChanged = (f != null ? (f.FirstName.CompareTo(r.FirstName) == 0
&& f.LastName.CompareTo(r.LastName) == 0) : false)
}).ToList();

关于c# - linq中的左外连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com