C# 在右表中找不到匹配项时,使用默认值左键联接两个表
我试图提出一个c#linq,在找不到匹配项时,用默认值设置左连接两个sql表 在月份表中,我有以下数据C# 在右表中找不到匹配项时,使用默认值左键联接两个表,c#,sql-server,linq,C#,Sql Server,Linq,我试图提出一个c#linq,在找不到匹配项时,用默认值设置左连接两个sql表 在月份表中,我有以下数据 MonthId Name 1 Jan 2 Feb 3 Mar 4 Apr 5 May mData包含以下数据 MonthId Count 1 10 2 20 3 5 我期待的结果如下 Name Count Jan 10 Feb
MonthId Name
1 Jan
2 Feb
3 Mar
4 Apr
5 May
mData包含以下数据
MonthId Count
1 10
2 20
3 5
我期待的结果如下
Name Count
Jan 10
Feb 20
Mar 5
Apr 0
May 0
我有下面的查询,它只在找到匹配项时返回
from p in Months
join g in mData on p.MonthId equals g.MonthId
select new {
p.Name,
g.Count
}
您需要将联接结果放入一个组中,并调用
DefaultIfEmpty
:
from p in Months
join g in mData on p.MonthId equals g.MonthId into monthData
from md in monthData.DefaultIfEmpty(new MData{Count = 0})
select new {
p.Name,
md.Count
}
DefaultIfEmpty()
的MSDN是否可能重复