C# 复杂LINQ查询与复杂for循环
现在我有了这个复杂的函数,我有一个main对象列表(称为MO),对于这些对象中的每一个,我都必须循环一个对象列表(称为C),其中包含标题、状态和子对象列表(称为E)。函数在这些子对象(E)上循环,并使用其标题和数量属性 函数的目标是创建一个字典(D1),其中键是C(标题),值是另一个字典(D2),键是E(标题),值是另一个字典(D3),键是C(状态),值是E(数量)。 因此,最后我将拥有所有(唯一的)C(标题),其中我可以看到所有(唯一的)E(标题),其中我可以看到所有不同的C(状态)和这些状态的E(数量)(额外的挑战是,如果两个E(数量)在同一课程上具有相同标题的相同状态,它们应该相互添加,然后作为值输入) 我把这一切都做好了 不过。函数很大,很难理解,所以我正在寻找一种更容易处理这个问题的方法。 其中一种方式应该是林克。然而,我对此几乎一无所知,对于一个如此复杂的函数,我几乎不知道如何在LINQ中处理它。 我还担心性能,因为这个WPF项目在很大程度上依赖于用户体验。所以我不确定LINQ是否真的能让事情变得更快、更慢或相同 这就是你们进来的地方C# 复杂LINQ查询与复杂for循环,c#,algorithm,linq,dictionary,C#,Algorithm,Linq,Dictionary,现在我有了这个复杂的函数,我有一个main对象列表(称为MO),对于这些对象中的每一个,我都必须循环一个对象列表(称为C),其中包含标题、状态和子对象列表(称为E)。函数在这些子对象(E)上循环,并使用其标题和数量属性 函数的目标是创建一个字典(D1),其中键是C(标题),值是另一个字典(D2),键是E(标题),值是另一个字典(D3),键是C(状态),值是E(数量)。 因此,最后我将拥有所有(唯一的)C(标题),其中我可以看到所有(唯一的)E(标题),其中我可以看到所有不同的C(状态)和这些状态
- LINQ是处理这个问题的更好方法吗
- 性能是否与我的功能类似
- LINQ查询更容易理解吗
- 是否有其他方法处理此复杂函数
而不是我描述的两种方法
在下面你会发现我用我的方式处理这个函数的函数 分3个步骤完成:
//DateTime start=DateTime.Now//启动性能测试
//开始->步骤1
List firstResultList=新列表();
foreach(MO列表中的MO-MO)
{
foreach(C在mo.listOfCs中)
{
字典D1=新字典();
int cStatus=c状态;
字典D2=新字典();
列表列表=新列表();
foreach(c.listOfEs中的E)
{
添加(e);
}
D2.添加(cStatus,eList);
D1.添加(c.标题,D2);
firstResultList.Add(D1);
}
}
//firstResultList=步骤1结果
//Console.WriteLine(firstResultList.ToString());
//
//步骤1->步骤2
Dictionary groupedDict=新字典();
foreach(firstResultList中的字典dict)
{
名单;
if(groupedDict.ContainsKey(dict.Keys.ElementAt(0)))
{
listje=分组数据通信[dict.Keys.ElementAt(0)];
}
其他的
{
listje=新列表();
}
添加(dict[dict.Keys.ElementAt(0)];
groupedDict[dict.Keys.ElementAt(0)]=listje;
}
//groupedDict=步骤2结果
//Console.WriteLine(groupedDict.ToString());
//
//步骤2->步骤3
Dictionary final=新字典();
int指数=0;
foreach(groupedDict.Values中的列表)
{
//在一个唯一的C中
List eStatusQuantityList=新列表();
foreach(列表中的词典dict)
{
foreach(在dict.Values中列出eList)
{
foreach(E在eList中)
{
如果(eStatusQuantityList.Count>0)
{
foreach(eStatusQuantityList中的字典dict2)
{
字典状态量化dict;
if(第2条容器(如标题))
{
statusQuantityDict=dict2[e.Title];
//int quantity=statusQuantityDict.value//statusQuantityDict[dict.Keys.ElementAt(0)];
整数数量=0;
int值;
bool hasValue=statusQuantityDict.TryGetValue(dict.Keys.ElementAt(0),out值);
if(hasValue){
数量=价值;
}否则{
//当价值不存在时做些什么
}
statusQuantityDict[dict.Keys.ElementAt(0)]=数量+e.数量;
dict2[e.Title]=状态数量dict;
}
其他的
{
statusQuantityDict=新字典();
statusQuantityDict.Add(dict.Keys.ElementAt(0),e.Quantity);
//DateTime start = DateTime.Now; //start performance test
//start -> step 1
List<Dictionary<string/*C(title)*/, Dictionary<int/*C(status)*/, List<E>>>> firstResultList = new List<Dictionary<string, Dictionary<int, List<E>>>>();
foreach(MO mo in listOfMOs)
{
foreach (C c in mo.listOfCs)
{
Dictionary<string, Dictionary<int, List<E>>> D1 = new Dictionary<string, Dictionary<int, List<E>>>();
int cStatus = c.status;
Dictionary<int, List<E>> D2 = new Dictionary<int, List<E>>();
List<E> eList = new List<E>();
foreach (E e in c.listOfEs)
{
eList.Add(e);
}
D2.Add(cStatus, eList);
D1.Add(c.Title, D2);
firstResultList.Add(D1);
}
}
//firstResultList = step1 results
//Console.WriteLine(firstResultList.ToString());
//
//step1 -> step2
Dictionary<string/*C(title)*/, List<Dictionary<int/*C(status)*/, List<E>>>> groupedDict = new Dictionary<string, List<Dictionary<int, List<E>>>>();
foreach (Dictionary<string, Dictionary<int, List<E>>> dict in firstResultList)
{
List<Dictionary<int, List<E>>> listje;
if(groupedDict.ContainsKey(dict.Keys.ElementAt(0)))
{
listje = groupedDict[dict.Keys.ElementAt(0)];
}
else
{
listje = new List<Dictionary<int, List<E>>>();
}
listje.Add(dict[dict.Keys.ElementAt(0)]);
groupedDict[dict.Keys.ElementAt(0)] = listje;
}
//groupedDict = step2 results
//Console.WriteLine(groupedDict.ToString());
//
//step2 -> step3
Dictionary<string/*C(title)*/, List<Dictionary<string/*E(title)*/, Dictionary<int/*C(status)*/, int/*E(quantity)*/>>>> final = new Dictionary<string, List<Dictionary<string, Dictionary<int, int>>>>();
int index = 0;
foreach (List<Dictionary<int, List<E>>> list in groupedDict.Values)
{
//Within one unique C
List<Dictionary<string, Dictionary<int, int>>> eStatusQuantityList = new List<Dictionary<string, Dictionary<int, int>>>();
foreach (Dictionary<int, List<E>> dict in list)
{
foreach (List<E> eList in dict.Values)
{
foreach(E e in eList)
{
if (eStatusQuantityList.Count > 0)
{
foreach (Dictionary<string, Dictionary<int, int>> dict2 in eStatusQuantityList)
{
Dictionary<int, int> statusQuantityDict;
if (dict2.ContainsKey(e.Title))
{
statusQuantityDict = dict2[e.Title];
//int quantity = statusQuantityDict.value//statusQuantityDict[dict.Keys.ElementAt(0)];
int quantity = 0;
int value;
bool hasValue = statusQuantityDict.TryGetValue(dict.Keys.ElementAt(0), out value);
if (hasValue) {
quantity = value;
} else {
// do something when the value is not there
}
statusQuantityDict[dict.Keys.ElementAt(0)] = quantity + e.Quantity;
dict2[e.Title] = statusQuantityDict;
}
else
{
statusQuantityDict = new Dictionary<int, int>();
statusQuantityDict.Add(dict.Keys.ElementAt(0), e.Quantity);
dict2.Add(e.Title, statusQuantityDict);
}
}
}
else
{
Dictionary<string, Dictionary<int, int>> test = new Dictionary<string, Dictionary<int, int>>();
Dictionary<int, int> test2 = new Dictionary<int, int>();
test2.Add(dict.Keys.ElementAt(0), e.Quantity);
test.Add(e.Title, test2);
eStatusQuantityList.Add(test);
}
}
}
}
//ending
string key = groupedDict.Keys.ElementAt(index);
final[key] = eStatusQuantityList;
index++;
//
}
//final contains step3 results
//Console.WriteLine(final.ToString());
/*
for (int i = 0; i<final.Keys.Count; i++)
{
Console.WriteLine(final.Keys.ElementAt(i));
}
for (int i = 0; i < final.Values.Count; i++)
{
Console.WriteLine(final.Values.ElementAt(i));
}
*/
//
//TimeSpan duration = DateTime.Now - start; //end performance test
//Console.WriteLine("That took " + duration.TotalMilliseconds + " ms"); //performance test results //60.006 is fine, 600.006 is OOM. //Our range of objects is max. 300 MO's though