也许您不需要先进行分组,但我不能保证对抗 Linq 的性能会更好。实际上 Linq 代码更简单,这通常是非常需要的。无论如何,为了完成,假设 quemRespondeu 是 long 类型:
public Dictionary<long,List<string>> GroupAnswers(List<Answer> AllAnswers)
{
long[] allIds = new long[AllAnswers.Count];
Answer[] AllAnswersArray = AllAnswers.ToArray();
for (int i = 0; i < AllAnswers.Count; i++)
allIds[i] = AllAnswers[i].quemRespondeu;
Array.Sort(allIds, AllAnswersArray);
Dictionary<long, List<string>> DictAllAnswersGrouped = new Dictionary<long, List<string>>();
List<string> ListCurrentUser = null;
long prevId = allIds[0] - 1;//to assure that ListCurrentUser is initialized in the first iteration
for (int i=0; i< allIds.Length; i++)
{
long Id = allIds[i];
if(Id!=prevId)
{
ListCurrentUser = new List<string>();
DictAllAnswersGrouped.Add(Id, ListCurrentUser);
prevId = Id;
}
ListCurrentUser.Add(AllAnswersArray[i].SurveyAnswer);
}
return DictAllAnswersGrouped;
}
然后你会调用:
Dictionary<long,List<string>> GroupedAnswersDict = GroupAnswers(surveyAnswers.Respostas);
long user_XId = 10; //whatever you need
List<string> userX_Answers;
if (!GroupedAnswersDict.TryGetValue(userX_Id, out userX_Answers))
userX_Answers = null;
免责声明: 正如我所说,Linq 代码更简单易读,因此在大多数情况下它应该是首选。尽管如此,在某些特定情况下可以选择上面的代码,例如,如果需要一些更复杂的功能,如果 Linq 不能使用,或者它对性能有任何实际好处。
实际上,出于好奇,我做了一个小测试,看看它的性能如何,并且在没有进一步优化的情况下,Linq 的性能通常优于这个算法(大约 2 倍),甚至将它与完全等效的算法进行比较:
var grouped = surveyAnswers.Respostas.GroupBy(x => x.quemRespondeu);
Dictionary<long, List<string>> GroupedAnswersDict =
grouped.ToDictionary(g => g.Key, g => g.Select(l => l.SurveyAnswer).ToList());
仅在每个用户的答案数量与用户数量相比非常少的情况下,该算法的性能优于上面的 Linq 代码(小于 1.2 倍),但仍然比分类分组慢(由 Supa 发布在这里,归功于他):
var grouped = surveyAnswers.Respostas.GroupBy(x => x.quemRespondeu);
Dictionary<long, List<Answer>> GroupedAnswersDict =
grouped.ToDictionary(g => g.Key, g => g.ToList());
所以在这种情况下,性能也不是选择它而不是 Linq 的理由。