Skip to main content

C# Tip of the Day 2020.01.15

15 Jan 2020 - Cody Merritt Anhorn

Want a quick and dirty way to randomly order a list in C#, use the OrderBy like below.

using System;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		var neatList = Enumerable.Range(1, 100).Select(
			o => new ComplexObject
			{
				Name = $"I am complex object {o}. ;)"
			}
		);
		var dirtyList = neatList.OrderBy(_ => Guid.NewGuid());
		Console.WriteLine("First in 'neatList': {0}", neatList.First().Name);
		Console.WriteLine("First in 'dirtyList': {0}", dirtyList.First().Name);
	}
	public class ComplexObject
	{
		public string Name { get; set; }
	}
}

Categories: blog dotnet

Tags: .NET C# Enumerable Tip of the Day


Cody Merritt Anhorn
Email: cody.anhorn@hotmail.com