【发布时间】:2022-01-07 21:44:39
【问题描述】:
某些代理在组字段中为空。 我正在尝试进行 LeftJoin,但收到类似 InnerJoin 的结果(仅限具有非空组的代理)
Agents = new ObservableCollection<dynamic>((await _repository.GetAgentsAsync() ?? new Agent[] { })
.Join(Groups.DefaultIfEmpty(), a => a.Group, g => g.ID, (a, g) =>
new { ID = a.ID, AgentName = a.AgentName, Login = a.Login, AgentID = a.AgentID, IsDel = a.IsDel, Group = g == null ? "Empty" : $"{g.NameGroup} ({g.Num})" }));
有什么问题?
谢谢大家,我找到答案https://stackoverflow.com/a/21584913/13618303
Groups = new ObservableCollection<Group>(await _repository.GetGroupsAsync() ?? new Group[] { });
Agents = new ObservableCollection<Agent>(await _repository.GetAgentsAsync() ?? new Agent[] { });
AgentsGroups = new ObservableCollection<dynamic>(Agents.GroupJoin(Groups, a => a.Group, g => g.ID, (a, g) => new { Agent = a, Group = g})
.SelectMany(ag => ag.Group.DefaultIfEmpty(), (a,g) => new { Agent = a.Agent, Group = g })
.Select ( ag => new { ID = ag.Agent.ID, AgentName = ag.Agent.AgentName, Login = ag.Agent.Login, AgentID = ag.Agent.AgentID, IsDel = ag.Agent.IsDel, Group = ag.Group == null ? "Empty" : $"{ag.Group.NameGroup} ({ag.Group.Num})" }));
【问题讨论】:
-
你是说组是空的吗? Agents 中会有 100 条唯一记录,Groups 中只有 80 条唯一记录? (所以 20 个代理没有组)
标签: c# linq left-join defaultifempty