Share via

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; }  
    }  
  
Developer technologies | C#
Developer technologies | 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.

0 comments No comments

Answer accepted by question author

Viorel 127K 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( );  

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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