某些代理在組欄位中為空。我正在嘗試執行 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})" }));
uj5u.com熱心網友回復:
方法語法不Join用于執行左連接樣式鏈接,它使用GroupJoin. 如果你直Join那么右邊(組)缺少一個元素將意味著左邊的元素(代理)消失。
GroupJoin 將左側的元素與右側的多個匹配元素配對
Left
1, John
2, Mary
3, Joe
Right
1, The Street, 1996
1, The Avenue, 2002
2, The Road, 2010
約翰住在兩個地方,瑪麗住在一個地方。喬從來沒有住在任何地方。Left.GroupJoin(Right)產生:
{1, John}, [ {1, The Street, 1996}, {1, The Avenue, 2002} ]
{2, Mary}, [ {1, The Road, 2010} ]
{3, Joe}, [ ]
GroupJoin 將保留左側的元素,如果沒有匹配,則將右側的元素設為空序列
如果您需要將其作為具有重復元素的串列回傳,您可以 SelectMany 它,它擴展了 person:addresses 所代表的串列串列。我會回到這個。
DefaultIfEmpty 不啟用左連接行為;如果呼叫它的序列具有零個元素,則回傳包含目標型別的一項(并具有該型別的默認值)的序列只是一種便利。本質上是這樣的:
public int[] DefaultIfEmpty(int[] input){
if(input.Length == 0)
return new int[1]; //a single integer, value 0 --> the default for int
else
return input;
}
您不需要在 Groups 上使用它;無論如何,這將是一個非操作,因為如果有 0 個組,將其轉換為單個null組的串列將表現相同(沒有代理會匹配)
如果您計劃在分組上使用 SelectMany,您確實想要DefaultIfEmpty,因為 SelectMany 根本不會對 0 個元素的集合進行操作。因為 Joe 沒有地址,SelectMany 會跳過他,他會從輸出中丟失
總而言之,這意味著您應該執行 a GroupJoin,并且您實際上不需要DefaultIfEmpty任何地方,因為您沒有執行SelectMany/ 您可以通過其他方式容忍代理有 0 個組。
我會使用更多的全名,因為不幸的是你加入了一個叫做 Group 的類(我猜),一個 groupjoin 給你一個代理串列,串列中的每個代理都有一個匹配組的串列:
.GroupJoin(Groups, agent => agent.Group, group => group.ID, (agent, groupCollection) =>
new {
agent.ID,
agent.AgentName,
agent.Login,
agent.AgentID,
agent.IsDel,
GroupNameAndNum = groupCollection.Select(g => $"{g.NameGroup} ({g.Num})").FirstOrDefault() ?? "Empty"
}
);
如果您是使用的SelectMany(也許你有有2組,并希望他們單獨列出,與代理資料重復代理),這將可能是這樣的:
.GroupJoin(Groups, agent => agent.Group, group => group.ID, (agent, groupCollection) => new { Agent = agent, OneOrMoreGroups = groupCollection.DefaultIfEmpty() })
.SelectMany(
agentWithGroupsCollection => agentWithGroupsCollection.OneOrMoreGroups,
(agentWithGroupsCollection, groupsCollectionItem) =>
new {
agentWithGroupsCollection.ID,
agentWithGroupsCollection.AgentName,
agentWithGroupsCollection.Login,
agentWithGroupsCollection.AgentID,
agentWithGroupsCollection.IsDel,
GroupNameAndNum = groupsCollectionItem == null ? "Empty" : $"{groupsCollectionItem.NameGroup} ({groupsCollectionItem.Num})"
}
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/370847.html
上一篇:修剪注釋中兩個字符之間的部分
