Home

Show dif names when diffing lists

Hi,

Is there an easy way to include the dif names when diffing two lists as opposed to two single objects?

For example this code:

void Main()
{
    var a = new House(1, 3);
    var b = new House(2, 3);
    Util.Dif(a, b).Dump();

    var firstList = new List<House>()
    {
        new House(1, 3),
        new House(2, 3)
    };
    var secondList = new List<House>()
    {
        new House(3, 3),
        new House(4, 3)
    };

    Util.Dif(firstList, secondList).Dump();
}

public class House(int number, int roomCount)
{
    public int Number => number;
    public int RoomCount => roomCount;

    public override string ToString()
    {
        return $"House {Number}";
    }
}

...produces this output:

It would be great to be able to see some dif names in the second dump. Maybe as additional Util.Dif string parameters?

Best regards

John

Comments

  • So my current workaround is this:

    void Main()
    {
        var a = new House(1, 3);
        var b = new House(2, 3);
        Util.Dif(a, b).Dump("Dump objects");
    
        var firstList = new List<House>()
        {
            new House(1, 3),
            new House(2, 3)
        };
        var secondList = new List<House>()
        {
            new House(3, 3),
            new House(4, 3)
        };
    
        Util.Dif(firstList, secondList).Dump("Dump lists");
    
        UtilEx.DifDump(firstList, secondList);
    }
    
    
    
    public class House(int number, int roomCount)
    {
        public int Number => number;
        public int RoomCount => roomCount;
    
        public override string ToString()
        {
            return $"House {Number}";
        }
    }
    
    
    
    public static class UtilEx
    {
        public static bool DifDump(object object1, object object2, int? depth = null, bool hideMatchingMembers = false,
                                    [CallerArgumentExpression(nameof(object1))] string leftLabel = "",
                                    [CallerArgumentExpression(nameof(object2))] string rightLabel = "")
        {
            var difResult = Util.Dif(object1, object2, depth, hideMatchingMembers);
            if (!difResult.IsSame)
            {
                var difLabels = new Div();
                difLabels.HtmlElement.InnerHtml = $"<td class='n'><span class='difremove'>{leftLabel}</span>&nbsp;<span class='difadd'>{rightLabel}</span></td>";
                difLabels.Dump();
            }       
            difResult.Dump(); 
            return difResult.IsSame;
        }
    }
    

    ... which results in this output:

    I don't love the mixing of the Diffing and Dump, but it's nice and compact at least:)

  • If the purpose is just to remind yourself which list is which, then I'd just use

    Util.Dif(firstList, secondList).DumpTell();

    or you can set Enable automatic Dump-headings by default for new queries and just dump as per normal.

  • Hi, I'd forgotten about DumpTell. I think I still want the colours to be reflected as I want the the most clear link between the labels and the data. DumpTell is cetrtainly quicker though :)

Sign In or Register to comment.