Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6916267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:34:47+00:00 2026-05-27T09:34:47+00:00

I am using LINQ Bridge targetting the .NET 2.0 Framework, and I am getting

  • 0

I am using LINQ Bridge targetting the .NET 2.0 Framework, and I am getting the following error. Just wanting the first element from a collection, chosen at random. Not concerned about performance in this particular case.

var result = someCollection.OrderBy(g => Guid.NewGuid()).Take(1).FirstOrDefault();

someCollection is a List<string>. The values in the collection are unique.

Unable to sort because the IComparer.Compare() method returns
inconsistent results. Either a value does not compare equal to itself,
or one value repeatedly compared to another value yields different
results. x: ”, x’s type: ‘Tuple2', IComparer:
'System.Array+FunctorComparer
1[LinqBridge.Tuple`2[System.String,System.Int32]]’.

But it seems to work just fine in .NET 4.0. Is there a workaround for this? Unfortunately I’m stuck using .NET 2.0 for this scenario.

EDIT Using the latest version of LINQ Bridge (1.2)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:34:48+00:00Added an answer on May 27, 2026 at 9:34 am

    Yet another update

    I found this question that has the same issue as you:
    Why does using Random in Sort causing [Unable to sort IComparer.Compare error]
    The problem is that LINQBridge uses List<>.Sort internally, which complains when using a “unstable” comparing algorithm, so you unfortunately can’t randomize this way.

    As an alternative, here’s some great code to randomize or to choose a random item:

        private static Random rnd = new Random();
        /// <summary>
        /// Chooses one of the items at random.
        /// 
        /// Returns default if there are no items.
        /// </summary>
        public static T RandomOrDefault<T>(this IEnumerable<T> source)
        {
            // We need the count:
            var buffer = source as ICollection<T> ?? source.ToList(); // (iterate only once)
            var itemCount = buffer.Count;
            if (itemCount == 0)
            {
                return default(T);
            }
    
            var index = rnd.Next(itemCount);
            return buffer.ElementAt(index);
        }
    
        /// <summary>
        /// Randomizes the order of the elements of a sequence. 
        /// </summary>
        public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
        {
            // This code is an implementation of the Fisher–Yates shuffle.
            // The code was obtained from:
            // https://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle-algorithm/1665080#1665080
            T[] elements = source.ToArray();
            // Note i > 0 to avoid final pointless iteration
            for (int i = elements.Length - 1; i > 0; i--)
            {
                // Swap element "i" with a random earlier element it (or itself)
                int swapIndex = rnd.Next(i + 1);
                yield return elements[swapIndex];
                elements[swapIndex] = elements[i];
                // we don't actually perform the swap; we can forget about the
                // swapped element because we already returned it.
            }
    
            // there is one item remaining that was not returned - we return it now
            yield return elements[0];
        }
    

    Update

    This exception really looks like a LINQBridge bug. I would recommend updating to the latest version. There’s no other apparent reason that you’re seeing this issue.

    Additional Info

    You can use a Random instead of Guid like so:

    var rnd = new Random();
    var result = someCollection.OrderBy(g => rnd.Next()).Take(1).FirstOrDefault();
    

    Also, .Take(1) is absolutely unnecessary when followed by .FirstOrDefault()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using LINQ to get data from a list I encounter this error. How
Using LINQ in .Net I can select items from an array that match a
Using Visual Studio 2010 .Net Framework 4 C# Linq to Entities Issue I would
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
Using Linq To XML, how can I get the space_id value (720) from the
Using LINQ on collections, what is the difference between the following lines of code?
Using LINQ to Entities, how can I determine if any item from a List
Using LINQ what is the best way to select a single item from a
Using LINQ I'm looking to break down the following path string[] , however I'd
Using Linq to Sql how do i group the following 2 tables. Orders Table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.