嵌套查询中的C#Linq Union Collections [英] C# Linq Union Collections in nested query
本文介绍了嵌套查询中的C#Linq Union Collections的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Linq表达这个片段,但是我不能全神贯注于它. _gameTableEntries
基本上是具有字段long tableId
和ICollection<object> connectedClients
I would like to express this snippet in Linq but I cannot wrap my mind around it. _gameTableEntries
is basically a tuple with fields long tableId
and ICollection<object> connectedClients
private ICollection<GameTableEntry> _gameTableEntries = new (...);
public ICollection<ConnectionHandler> GetConnectedClients(long tableId)
{
HashSet<ConnectionHandler> set = new HashSet<ConnectionHandler>();
foreach (var tableEntry in _gameTableEntries)
{
if (!tableEntry.TableId.Equals(tableId)) continue;
foreach (var handler in tableEntry.ConnectedClients)
{
if (!handler.IsConnected) continue;
set.Add(handler);
}
}
return set;
}
推荐答案
You are possibly after something like this with SelectMany
将序列的每个元素投影到IEnumerable并展平 产生的序列变成一个序列.
Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.
public ICollection<ConnectionHandler> GetConnectedClients(long tableId)
=> _gameTableEntries.Where(x => x.TableId == tableId)
.SelectMany(x => x.ConnectedClients)
.Where(x => x.IsConnected)
.ToHashSet();
这篇关于嵌套查询中的C#Linq Union Collections的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文