使用Linq检查字典中的泛型列表是否包含值
如何检查字典是否已包含一组坐标?我正在使用SelectManager和Where,但计数仍然错误。我不认为它检查了通用列表中的值 我希望它遍历matchTile,并检查通用列表中的其他元素是否存在条件(不是问题的一部分) 如果它满足该条件,它会将其添加到同一组中。当下一次迭代到来时,它应该检查该元素是否已经添加到组中。如果是,则跳过它。如果没有,它将创建一个新组(列表) 有人能帮忙吗使用Linq检查字典中的泛型列表是否包含值,linq,dictionary,foreach,generic-list,Linq,Dictionary,Foreach,Generic List,如何检查字典是否已包含一组坐标?我正在使用SelectManager和Where,但计数仍然错误。我不认为它检查了通用列表中的值 我希望它遍历matchTile,并检查通用列表中的其他元素是否存在条件(不是问题的一部分) 如果它满足该条件,它会将其添加到同一组中。当下一次迭代到来时,它应该检查该元素是否已经添加到组中。如果是,则跳过它。如果没有,它将创建一个新组(列表) 有人能帮忙吗 private void groupMatches(List<int[]> matchTile){
private void groupMatches(List<int[]> matchTile){
Dictionary<int, List<int[]>> groups = new Dictionary<int, List<int[]>>();
int i = 0;
foreach(int[] coord in matchTile){
var alreadyCounted = groups.SelectMany(x => x.Value).Where(x => x[0] == coord[0] && x[1] == coord[1]);
Debug.Log ("counted: " + alreadyCounted.Count ());
if(alreadyCounted.Count() > 0) continue;
// Create new group
groups.Add(i, new List<int[]>());
groups[i].Add(coord);
foreach(int[] nextCoord in matchTile){
if (...)
{
groups[i].Add(nextCoord);
}
}
i++;
}
Debug.Log ("groups: " + groups.Count);
}
private void groupMatches(列表匹配块){
字典组=新字典();
int i=0;
foreach(匹配块中的int[]坐标){
var alreadyCounted=groups.SelectMany(x=>x.Value)。其中(x=>x[0]==coord[0]&&x[1]==coord[1]);
Log(“counted:+alreadyCounted.Count());
如果(alreadyCounted.Count()>0)继续;
//创建新组
添加(i,新列表());
组[i]。添加(坐标);
foreach(匹配块中的int[]下一个命令){
如果(…)
{
组[i]。添加(下一个命令);
}
}
i++;
}
Debug.Log(“组:“+groups.Count”);
}
尝试下一个代码:
private void groupMatches(List<int[]> matchTile)
{
Dictionary<int, List<int[]>> groups
= matchTile.GroupBy(line => new{x = line[0], y = line[1]})
.Select((grp, index) => new{index, grp})
.ToDictionary(info => info.index, info => info.grp.ToList());
Debug.Log("groups: " + groups.Count);
}
private void groupMatches(列表匹配块)
{
字典组
=matchTile.GroupBy(line=>new{x=line[0],y=line[1]})
.Select((grp,index)=>new{index,grp})
.ToDictionary(info=>info.index,info=>info.grp.ToList());
Debug.Log(“组:“+groups.Count”);
}