none
How to compare dynamic objects array (Entity Type)? RRS feed

  • Question

  • I want to compare two dynamic (entity type from EF) objects array and I have to save data for the changed field's value.

    But I can't compare the values at runtime . I have attached the code bellow. 

     public string CompareDataObject(dynamic oldObject, dynamic newObject)
            {
                foreach (var source in oldObject)
                {
                    foreach (var destination in newObject)
                    {
    /*
    Compare code
    */
                       }
            }
       }

    It will be helpful form me if someone help me out to get the "Compare code" block.

    Thanks

    Tanvir.

    Monday, December 9, 2013 8:43 AM

Answers

  • Equality for dynamic types should work fine. Not sure if I missunderstood your question, but doing something like:

    class Program
    {
        static void Main(string[] args)
        {
            var first = new dynamic[] { "FirstTest1", new object(), "FirstTest3" };
            var second = new dynamic[] { "SecondTest1", "SecondTest2", "FirstTest3" };
    
            CompareDataObject(first, second);
    
            Console.ReadLine();
        }
    
        public static void CompareDataObject(dynamic oldObject, dynamic newObject)
        {
            foreach (var source in oldObject)
            {
                foreach (var destination in newObject)
                {
                    Console.WriteLine("source: {0}, destination: {1}, Are equal: {2}",  source,  destination, source == destination);
                }
            }
        }
    }

    Returns that in the last iteration the objects are equal.

    HTH

    • Proposed as answer by Eason_H Monday, December 16, 2013 2:24 AM
    • Marked as answer by Eason_H Tuesday, December 17, 2013 1:38 AM
    Monday, December 9, 2013 9:12 AM

All replies

  • Equality for dynamic types should work fine. Not sure if I missunderstood your question, but doing something like:

    class Program
    {
        static void Main(string[] args)
        {
            var first = new dynamic[] { "FirstTest1", new object(), "FirstTest3" };
            var second = new dynamic[] { "SecondTest1", "SecondTest2", "FirstTest3" };
    
            CompareDataObject(first, second);
    
            Console.ReadLine();
        }
    
        public static void CompareDataObject(dynamic oldObject, dynamic newObject)
        {
            foreach (var source in oldObject)
            {
                foreach (var destination in newObject)
                {
                    Console.WriteLine("source: {0}, destination: {1}, Are equal: {2}",  source,  destination, source == destination);
                }
            }
        }
    }

    Returns that in the last iteration the objects are equal.

    HTH

    • Proposed as answer by Eason_H Monday, December 16, 2013 2:24 AM
    • Marked as answer by Eason_H Tuesday, December 17, 2013 1:38 AM
    Monday, December 9, 2013 9:12 AM
  • Hello

    Thanks for your reply. actually the fact is my object is entity type and complex apart from the example which you have provided. I have to take the object's property as well as value from the dynamic object to preserve the change. Please take a look at the attached image :

    for example if the "Comments" property change somehow then I have to store it like "Comments#no co"

    Hope now the issue is clear.

    Looking forward towards your response.

    Thanks

    Tanvir.

    Monday, December 9, 2013 9:44 AM
  • Hi Tanvir,

    Why do you want to do that? Where is the  the "Comments" property ? I think David had help you to get the "Compare code" block.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 16, 2013 2:26 AM