Merging or "Joining" generic lists

Fullstop 21 Reputation points
2022-09-18T13:16:17.537+00:00

Hello
I have 2 generic lists <T> that are identical in design
NRlzAA
I need to “merge or join” these so I can compare the FieldValue between them for equality.
Basically, the result of the merger should contain the differences between the lists and delivered in the result class.

The sample data provided in the fiddle differs at

ID= Guid.Parse ("d3ce2687-e1ca-4513-a4bb-357e42a6e817"),  
                    FiledName="Int",  
                    FieldValue="7777777"  

How can I do this, can someone help?

  public class Result  
    {  
        public Guid ID { get; set; }  
        public string FiledName { get; set; }  
        public object FieldValueBeforeChange { get; set; }  
        public object FieldValueAfterChange { get; set; }  
    }  
  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,822 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2022-09-18T13:59:12.397+00:00

    Try something like this:

    List<Result> result = beforeChange  
        .Join( afterChange, b => new { b.ID, b.FieldName }, a => new { a.ID, a.FieldName }, ( b, a ) => new { b, a } )  
        .Where( r => !object.Equals( r.b.FieldValue, r.a.FieldValue ) )  
        .Select( r => new Result { ID = r.b.ID, FieldName = r.b.FieldName, FieldValueBeforeChange = r.b.FieldValue, FieldValueAfterChange = r.a.FieldValue } )  
        .ToList( );  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.