ForEach for List List in Linq

I am trying to do some list list operations.

I have a list with some properties. I have divided this list into sub-lists based on the GroupId property.

I list all the lists in the list to another class. but here I had to use 2 ForEachs. is there a way to do this in one so that I can increase performance when it comes to large input of more than 10,000 list items in an input list. below is the code i tried

 class Comp
 {
    public int CompId { get; set; }
    public string CompName { get; set; }
    public int GroupId { get; set; }
 }

 class EmpComp
 {
        public int EmpCompId { get; set; }
        public string EmpCompName { get; set; }
        public int EmpId { get; set; }
        public int GroupId { get; set; }
 }

    #region Input

    List<Comp> compList = new List<Comp>();
    compList.Add(new Comp { CompId = 1, CompName = "One", GroupId = 1 });
    compList.Add(new Comp { CompId = 2, CompName = "Two", GroupId = 1 });
    compList.Add(new Comp { CompId = 3, CompName = "One", GroupId = 2 });
    compList.Add(new Comp { CompId = 4, CompName = "Three", GroupId = 1 });
    compList.Add(new Comp { CompId = 5, CompName = "One", GroupId = 4 });
    compList.Add(new Comp { CompId = 6, CompName = "Two", GroupId = 4 }); 

    #endregion

    var groupedCompList = compList.GroupBy(u => u.GroupId ).Select(grp => grp.ToList()).ToList();
    List<EmpComp> empCompList = new List<EmpComp>();
    int empId = 0;//Just for reference
    groupedCompList.ForEach(x =>
    {
        x.ForEach(y =>
        {
            EmpComp empComp = new EmpComp();
            empComp.EmpCompId = y.CompId;
            empComp.EmpCompName = y.CompName;
            empComp.GroupId = y.GroupId ;
            empComp.EmpId = empId + 1;
            empCompList.Add(empComp);
        });
        empId++;
    });

I want to avoid using two ForEachs here.

Note. I have other identifiers and strings that need to be assigned based on GroupId. empId is just an example

+4
source share
3

ForEach SelectMany, ForEach s. , DisplayOrder GroupBy - , DisplayOrder . , .

- ForEach, :

List<EmpComp> empCompList =
    compList
        .OrderBy(u => u.DisplayOrder)
        .Select((y, n) => new EmpComp()
        {
            EmpCompId = y.CompId,
            EmpCompName = y.CompName,
            DisplayOrder = y.DisplayOrder,
            EmpId = n + 1,
        }).ToList();

, empId++; .

, :

List<EmpComp> empCompList =
    compList
        .OrderBy(u => u.DisplayOrder)
        .GroupBy(u => u.DisplayOrder)
        .Select((ys, n) =>
            ys
                .Select(y => new EmpComp()
                {
                    EmpCompId = y.CompId,
                    EmpCompName = y.CompName,
                    DisplayOrder = y.DisplayOrder,
                    EmpId = n + 1,
                }))
        .SelectMany(x => x)
        .ToList();

:

empCompList

+3

( Enigmativity):

int empId=0;
Dictionary<string, int> displayOrderTable = compList.GroupBy(u => u.DisplayOrder).Distinct().ToDictionary(x=>x.Key, (v,k)=>empId++;)
List<EmpComp> empCompList =
    compList
        .OrderBy(u => u.DisplayOrder)
        .Select((y, n) => new EmpComp()
        {
            EmpCompId = y.CompId,
            EmpCompName = y.CompName,
            DisplayOrder = y.DisplayOrder,
            EmpId = displayOrderTable[y.DisplayOrder],
        }).ToList();

O (N + N) = O (N) , O (N ^ 2) ( for in for).

+1

, LINQ

var empCompList = compList
    .GroupBy(c => c.GroupId)
    .SelectMany((g, i) => g.Select(c => new EmpComp
    {
        EmpCompId = c.CompId,
        EmpCompName = c.CompName,
        GroupId = c.GroupId,
        EmpId = i + 1
    }))
    .ToList();

The key point is SelectMany overload , which gets the source element and index.

+1
source

Source: https://habr.com/ru/post/1619159/


All Articles