Home > Net >  "Merging" or " Joining" Generic Lists
"Merging" or " Joining" Generic Lists

Time:09-21

I have 2 generic lists that are identical in design; Fiddle

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"
  public class Result
{
    public Guid ID { get; set; }
    public string FiledName { get; set; }
    public object FieldValueBeforeChange { get; set; }
    public object FieldValueAfterChange { get; set; }
}

CodePudding user response:

This is what I was looking for. If it could help someone ...

 List<Result> result = beforeChange
 .Join( afterChange, a => new { a.ID, a.FieldName }, b => new { b.ID, b.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( );
  • Related