SequenceEqual does not work below in comparing class












0















I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.



        public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}

List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();

product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});

product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});


if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}









share|improve this question























  • Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.

    – jonsca
    Jan 2 at 5:06
















0















I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.



        public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}

List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();

product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});

product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});


if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}









share|improve this question























  • Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.

    – jonsca
    Jan 2 at 5:06














0












0








0








I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.



        public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}

List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();

product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});

product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});


if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}









share|improve this question














I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.



        public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}

List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();

product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});

product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});


if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}






c# generics asp.net-core .net-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 4:55









HardyWestHardyWest

419




419













  • Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.

    – jonsca
    Jan 2 at 5:06



















  • Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.

    – jonsca
    Jan 2 at 5:06

















Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.

– jonsca
Jan 2 at 5:06





Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.

– jonsca
Jan 2 at 5:06












2 Answers
2






active

oldest

votes


















3














SequenceEqual compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product references.



If you change your code to use the same Product in each list then SequenceEqual will return `true'



var product = new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
};

product1.Add(p);
product2.Add(p);


if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}


Why? Because in this example the lists contain the same object.



Read this for more information on how SequenceEqual compares the two sequences.



If you want SequenceEqual to return true if the properties of the Product class are the same then you need to override the Equals method on the Product class.



EDIT



As others have mentioned, it is a better practice to implement the IEqualityComparer<T> interface instead of directly overriding the Equals method.






share|improve this answer

































    2














    By default, the SequenceEqual method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.



    If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):




    • have the Product class implement the IEquatable<Product> interface; or

    • create a separate class that implements the IEqualityComparer<Product> interface and use the overload of SequenceEqual that takes an instance of IEqualityComparer<T>


    I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001392%2fsequenceequal-does-not-work-below-in-comparing-class%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      SequenceEqual compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product references.



      If you change your code to use the same Product in each list then SequenceEqual will return `true'



      var product = new Product
      {
      ProductId = 1,
      ProductName = "TV",
      ProductDescription = "Television Set"
      };

      product1.Add(p);
      product2.Add(p);


      if (product1.SequenceEqual(product2))
      {
      Console.WriteLine("equal data");
      }
      else
      {
      Console.WriteLine("Not equal data");
      }


      Why? Because in this example the lists contain the same object.



      Read this for more information on how SequenceEqual compares the two sequences.



      If you want SequenceEqual to return true if the properties of the Product class are the same then you need to override the Equals method on the Product class.



      EDIT



      As others have mentioned, it is a better practice to implement the IEqualityComparer<T> interface instead of directly overriding the Equals method.






      share|improve this answer






























        3














        SequenceEqual compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product references.



        If you change your code to use the same Product in each list then SequenceEqual will return `true'



        var product = new Product
        {
        ProductId = 1,
        ProductName = "TV",
        ProductDescription = "Television Set"
        };

        product1.Add(p);
        product2.Add(p);


        if (product1.SequenceEqual(product2))
        {
        Console.WriteLine("equal data");
        }
        else
        {
        Console.WriteLine("Not equal data");
        }


        Why? Because in this example the lists contain the same object.



        Read this for more information on how SequenceEqual compares the two sequences.



        If you want SequenceEqual to return true if the properties of the Product class are the same then you need to override the Equals method on the Product class.



        EDIT



        As others have mentioned, it is a better practice to implement the IEqualityComparer<T> interface instead of directly overriding the Equals method.






        share|improve this answer




























          3












          3








          3







          SequenceEqual compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product references.



          If you change your code to use the same Product in each list then SequenceEqual will return `true'



          var product = new Product
          {
          ProductId = 1,
          ProductName = "TV",
          ProductDescription = "Television Set"
          };

          product1.Add(p);
          product2.Add(p);


          if (product1.SequenceEqual(product2))
          {
          Console.WriteLine("equal data");
          }
          else
          {
          Console.WriteLine("Not equal data");
          }


          Why? Because in this example the lists contain the same object.



          Read this for more information on how SequenceEqual compares the two sequences.



          If you want SequenceEqual to return true if the properties of the Product class are the same then you need to override the Equals method on the Product class.



          EDIT



          As others have mentioned, it is a better practice to implement the IEqualityComparer<T> interface instead of directly overriding the Equals method.






          share|improve this answer















          SequenceEqual compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product references.



          If you change your code to use the same Product in each list then SequenceEqual will return `true'



          var product = new Product
          {
          ProductId = 1,
          ProductName = "TV",
          ProductDescription = "Television Set"
          };

          product1.Add(p);
          product2.Add(p);


          if (product1.SequenceEqual(product2))
          {
          Console.WriteLine("equal data");
          }
          else
          {
          Console.WriteLine("Not equal data");
          }


          Why? Because in this example the lists contain the same object.



          Read this for more information on how SequenceEqual compares the two sequences.



          If you want SequenceEqual to return true if the properties of the Product class are the same then you need to override the Equals method on the Product class.



          EDIT



          As others have mentioned, it is a better practice to implement the IEqualityComparer<T> interface instead of directly overriding the Equals method.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 5:17

























          answered Jan 2 at 5:06









          Simply GedSimply Ged

          2,67621523




          2,67621523

























              2














              By default, the SequenceEqual method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.



              If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):




              • have the Product class implement the IEquatable<Product> interface; or

              • create a separate class that implements the IEqualityComparer<Product> interface and use the overload of SequenceEqual that takes an instance of IEqualityComparer<T>


              I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.






              share|improve this answer




























                2














                By default, the SequenceEqual method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.



                If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):




                • have the Product class implement the IEquatable<Product> interface; or

                • create a separate class that implements the IEqualityComparer<Product> interface and use the overload of SequenceEqual that takes an instance of IEqualityComparer<T>


                I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.






                share|improve this answer


























                  2












                  2








                  2







                  By default, the SequenceEqual method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.



                  If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):




                  • have the Product class implement the IEquatable<Product> interface; or

                  • create a separate class that implements the IEqualityComparer<Product> interface and use the overload of SequenceEqual that takes an instance of IEqualityComparer<T>


                  I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.






                  share|improve this answer













                  By default, the SequenceEqual method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.



                  If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):




                  • have the Product class implement the IEquatable<Product> interface; or

                  • create a separate class that implements the IEqualityComparer<Product> interface and use the overload of SequenceEqual that takes an instance of IEqualityComparer<T>


                  I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 5:07









                  Mickaël DerrieyMickaël Derriey

                  4,8822440




                  4,8822440






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001392%2fsequenceequal-does-not-work-below-in-comparing-class%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas