Solved

SequenceEqual in C# List

Posted on 2016-09-21
2
127 Views
Last Modified: 2016-09-21
Hi,

I have 2 List<string>: list_A and list_B.
it has 3 items inside each.

I got the values from api call and they are:
Iist_A[0] : "Hello"
list_A[1]: "I am <br><br> Isabell"
list_A[2]: "Sincerely, <br><br>Isabell"

I retrieved the values from DB and stored those to list_B.
I added and deleted items on list_B since I had to manipulate the data and string.
So when I finally added items, the previously stored items were overwritten by the new ones.
However, at the end, this is what I got for list_B.
Iist_B[0] : "Hello"
list_B[1]: "I am <br><br> Isabell"
list_B[2]: "Sincerely, <br><br>Isabell"

When I checked the values using Locals window in Visual Studio, I verified that all the values were matched. I even used 'Text Visualizer', but the results were exactly matched.
However I am getting "false" on the following statement.

list_A.SequenceEqual(list_B);

Open in new window


How could this happen and how to fix this issue?
0
Comment
Question by:Isabell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 250 total points
ID: 41809908
it should be equals and returned true if you compare to 2 same Lists

try check the 1st and 2nd results below
List<string> list_A = new List<string> { "Hello", "I am <br><br> Isabell", "Sincerely, <br><br>Isabell" };
            List<string> list_B = new List<string> { "Hello", "I am <br><br> Isabell", "Sincerely, <br><br>Isabell" };

            Boolean b = list_A.SequenceEqual(list_B);
            Response.Write("B = " + b.ToString() + "<br>");

            list_B.Add("test");
            b = list_A.SequenceEqual(list_B);
            Response.Write("B = " + b.ToString() + "<br>");

Open in new window

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 41809946
I have copied your values to my code (below) - and it shows that lists are identical...:

            List<string> list_A = new List<string>();
            list_A.Add("Hello");
            list_A.Add("I am <br><br> Isabell");
            list_A.Add("Sincerely, <br><br>Isabell");
            List<string> list_B = new List<string>();
            list_B.Add("Hello");
            list_B.Add("I am <br><br> Isabell");
            list_B.Add("Sincerely, <br><br>Isabell");

            MessageBox.Show((list_A.SequenceEqual(list_B)).ToString());

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question