11,428,497 members (63,516 online)

# The Elegant Art of Programming

, 15 May 2012 CPOL
 Rate this:
This tip uses the elegant LINQ to solve pesky daily problems.

## Introduction

One of the most rewarding experiences of computer programming is to find more elegant ways of solving a problem. In this tip, I would like to first describe a realistic problem. Then show a longer way of solving it (dare I say, intuitive?) Finally show an elegant way of solving it in Microsoft LINQ.

The purpose of this tip is not to explain the details of LINQ, but show how it can be used in a simple to understand context. Reading the code should be very straightforward.

## Problem

Imagine you have a list of double numbers, and we want to know if all the numbers in the list are the same, or not.

## Solution 1

A simple console application in C# would solve this problem using a flag, a for loop, and a couple of if statements:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solution1
{
class Program
{

static void Main(string[] args)
{
List<double> myList = new List<double>();

bool isSame = true;
double track = -1.0;
foreach (double num in myList)
{
if (track < 0.0)
{
track = num;
}
else
{
if (track != num)
{
isSame = false;
break;
}
}
}

if (isSame)
{
Console.WriteLine("All numbers are the same.");
}
else
{
Console.WriteLine("All numbers are not the same.");
}

}
}
}

## Solution 2

Even if you have seen LINQ in action before, it's still fun to see it again:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solution2
{
class Program
{
static void Main(string[] args)
{
List<double> myList = new List<double>();

var query1 = (from num in myList select num).Distinct();

if (query1.Count() > 1)
{
Console.WriteLine("All numbers are not the same.");
}
else
{
Console.WriteLine("All numbers are the same.");
}

}
}
}

## Points of Interest

While both programs do the same thing, the solution in LINQ is an order of magnitude times smaller. It focuses on the algorithm and not the loop, as Bjarne Stroustrup encourages all programmers to do so. Attached are the projects for solutions 1 and 2.

## Share

Software Developer (Senior)
United States
No Biography provided

 First Prev Next
 My vote of 3 [modified] Guillaume Leparmentier18-May-12 10:28 Guillaume Leparmentier 18-May-12 10:28
 Re: My vote of 3 Erol Esen18-May-12 11:17 Erol Esen 18-May-12 11:17
 Re: My vote of 3 Guillaume Leparmentier18-May-12 11:58 Guillaume Leparmentier 18-May-12 11:58
 Re: My vote of 3 Erol Esen20-May-12 14:36 Erol Esen 20-May-12 14:36
 yet another solution... Andreas Gieriet15-May-12 6:58 Andreas Gieriet 15-May-12 6:58
 Re: yet another solution... PIEBALDconsult15-May-12 9:08 PIEBALDconsult 15-May-12 9:08
 Re: yet another solution... [modified] Andreas Gieriet15-May-12 11:25 Andreas Gieriet 15-May-12 11:25
 Re: yet another solution... Erol Esen15-May-12 12:18 Erol Esen 15-May-12 12:18
 Re: yet another solution... PIEBALDconsult15-May-12 13:46 PIEBALDconsult 15-May-12 13:46
 Re: yet another solution... Andreas Gieriet15-May-12 14:03 Andreas Gieriet 15-May-12 14:03
 Re: yet another solution... PIEBALDconsult15-May-12 15:28 PIEBALDconsult 15-May-12 15:28
 Re: yet another solution... Andreas Gieriet16-May-12 0:40 Andreas Gieriet 16-May-12 0:40
 even simpler Andreas Gieriet15-May-12 6:32 Andreas Gieriet 15-May-12 6:32
 Don't do that with real values (double, float, decimals)! Andreas Gieriet15-May-12 6:03 Andreas Gieriet 15-May-12 6:03
 Re: Don't do that with real values (double, float, decimals)! Erol Esen15-May-12 6:30 Erol Esen 15-May-12 6:30
 Re: Don't do that with real values (double, float, decimals)! Andreas Gieriet15-May-12 6:43 Andreas Gieriet 15-May-12 6:43
 Alternatives... Andrew Rissing15-May-12 5:42 Andrew Rissing 15-May-12 5:42
 Re: Alternatives... Erol Esen15-May-12 6:25 Erol Esen 15-May-12 6:25
 ..or .. ednrg15-May-12 5:36 ednrg 15-May-12 5:36
 Re: ..or .. delscorcho1@gmail.com17-May-12 7:22 delscorcho1@gmail.com 17-May-12 7:22
 Last Visit: 31-Dec-99 19:00     Last Update: 3-May-15 4:25 Refresh 1