How do I compare IOrderedEnumerable<char> in C#?

Hi,

I have the following code to check if two string are the same after they are re-ordered.
I want to use LINQ and this is what I did.
when I print s1 and s2 using foreach...they are both "eilnst". But it returns "false".

How can I correct this?

static void Main(string[] args)
        {
            string str1 = "Silent";
            string str2 = "Listen";
            Console.WriteLine(areEqual(str1, str2));
            Console.Read();
        }
        
        public static bool areEqual(string str1, string str2)
        {
            var s1 = str1.ToLower().OrderBy(x => x);
            var s2 = str1.ToLower().OrderBy(x => x);
            foreach (var item in s1)
            {
                Console.Write(item);

            }
            Console.WriteLine();
            foreach (var item in s2)
            {
                Console.Write(item);
            }
            Console.WriteLine();

            return  str1.ToLower().OrderBy(x => x)==(str1.ToLower().ToList().OrderBy(x => x));          
        }

Open in new window

IzzyTwinklyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You are comparing two IEnumerable instances by reference. These would never be equal unless they are the exact same reference. You could try using the SequenceEqual method:

return Enumerable.SequenceEqual(str1.ToLower().OrderBy(x => x), str1.ToLower().OrderBy(x => x));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
Alternatively, you could convert your IEnumerable<char> to a string and compare the results:

var s1 = string.Concat(str1.ToLower().OrderBy(x => x).TakeWhile(char.IsLetter));
var s2 = string.Concat(str2.ToLower().OrderBy(x => x).TakeWhile(char.IsLetter));

return s1 == s2;

Open in new window

0
sarabandeCommented:
           var s1 = str1.ToLower().OrderBy(x => x);
            var s2 = str1.ToLower().OrderBy(x => x);
there is a typo. you were using str1 for both statements.  (it was corrected in the code Chris has posted).

Sara
1
sarabandeCommented:
There are given valid solutions and one correction.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.