Как уменьшить объём кода? - C#
Формулировка задачи:
int[] listIdClassGroupsForAllowToUser=...;
var temp1 = uow.GetRepository<Classification>() .All() .Include( clf => clf.ClassGroups.Where(cg => listIdClassGroupsForAllowToUser.Contains(cg.ClassGroupID)));
var temp1 = uow.GetRepository<Classification>().All()
.Include(clf =>
clf.ClassGroups);
var temp2 = new List<Classification>();
foreach (var t1 in temp1)
{
var temp21 = new Classification()
{
Name = t1.Name,
ClassGroupCodeLength = t1.ClassGroupCodeLength,
ClassificationID = t1.ClassificationID,
ClassGroups = new List<ClassGroup>()
};
foreach (var t2 in t1.ClassGroups)
{
if (listIdClassGroupsForAllowToUser.Contains(t2.ClassGroupID))
{
temp21.ClassGroups.Add(t2);
AddParentClassGroup(ref temp22, t2.ParentClassGroup);
}
}
temp2.Add(temp21);
}
public void AddParentClassGroup(ref List<ClassGroup> listCG, ClassGroup cg)
{
if (!listCG.Contains(cg))
listCG.Add(cg);
if(cg !=null)
{
if (cg.ParentClassGroup != null && cg.ParentClassGroupID != 0)
AddParentClassGroup(ref listCG, cg.ParentClassGroup);
}
}Решение задачи: «Как уменьшить объём кода?»
textual
Листинг программы
var temp1 = uow.GetRepository<Classification>() .All() .Include( clf => clf.ClassGroups).Where(cg => listIdClassGroupsForAllowToUser.Intersect(cg.ClassGroups.Select(s=>s.ClassGroupID)).Count()>0)));