Skip to main content
2 of 3
edited body
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

You can definitely use the collection initializer showed by @Paparazzi

var processDetails = new List<OnGoingProcess>()
{ 
    new OnGoingProcess(120, "Process 29", "T20"), 
    new OnGoingProcess(85, "Process 229")
};

Like others I have difficulties to understand the requirements but I think what you are looking for is a Union

Produces the set union of two sequences by using a specified IEqualityComparer.

With it you can replace the entire foreach as

var mergedProcesses = processDetails.Union(routePlans, new ProcessEqualityComparer());

where

class ProcessEqualityComparer : IEqualityComparer<OnGoingProcess>
{
    public bool Equals(OnGoingProcess left, OnGoingProcess right) 
    {
        return left.Id == right.Id;
    }

    public int GetHashCode(OnGoingProcess process)
    {
        return process.Id.GetHashCode();
    }
}

The result is:

120 Process 29  T20
100 Process 2   A20
99  Process 3   S20
85  Process 229 B20
220 Process 39  C20
15  Process 20  D20
101 Process 129 G20
222 Process new null
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190