Best linq questions in April 2012

Why is "Divide by Zero" or any other exception not raised?

17 votes

I have a double[] on which a LINQ operation is being performed:

MD = MD.Select(n => n * 100 / MD.Sum()).ToArray();

In some cases, all elements of MD are 0 and then Sum is also zero. Then 0 * 100 = 0 / 0, but it is not giving a divide-by-zero exception or any exception. Why is this so?

Try this:

double x = 0.0;
double y = 1.0;
double z = y / x;

That won't throw an exception either: it'll leave z as positive infinity. There's nothing LINQ-specific here - it's just IEEE-754 floating point arithmetic behaviour.

In your case, you're dividing zero by zero, so you end up with not-a-number.

Why is Array.Sort() so slow compared to LINQ?

17 votes

I made quick testing application to compare LINQ sorting to Array.Sort on my custom objects. Array.Sort seems extremely slow!

I made my custom class like this:

class Person : IComparable<Person>
{
    public int Age { get; set; }
    public string Name { get; set; }

    public int CompareTo(Person obj)
    {
        return this.Age.CompareTo(obj.Age);
    }

    public Person()
    { }

}

Then i made my testing persons in main() :

string name = "Mr. Tomek";

Random r = new Random();
int size = 10000000;

DateTime start, end;
Person[] people1 = new Person[size];
Person[] people2 = new Person[size];

for (int i = 0; i < size; i++)
{
     people1[i] = new Person();
     people1[i].Age = r.Next(0, 10000);
     people1[i].Name = name;

     people2[i] = new Person();
     people2[i].Age = people1[i].Age;
     people2[i].Name = people1[i].Name;
}

After that i have measured time taken to Sort by Array.Sort and by LINQ:

start = DateTime.Now;
var sort = from s in people2
           orderby s.Age
           select s;
end = DateTime.Now;
Console.WriteLine("LINQ: ");
Console.WriteLine((end - start).TotalMilliseconds);

start = DateTime.Now;
Array.Sort(people1,((Person p1, Person p2)=>{return p1.CompareTo(p2);}));
end = DateTime.Now;

Console.WriteLine("IComparable: ");
Console.WriteLine((end - start).TotalMilliseconds);

Console.ReadLine();

Linq time: about 1 or 2 miliseconds

Array.Sort: over 16 SECONDS!

All arrays are sorted (LINQ produces new collection and leaves oryginal array unsorted) but Array.Sort is extremely slow! How could it be explained? (in DEBUG and RELEASE mode Array.Sort fails extremely)

I pasted code with lambda expression when sorting with Array.Sort but it's the same with and without it. (Class Person implements IComparable interface)

Your Linq query doesn't even get executed because you don't get the results. That's called deferred execution. The query is only executed when you actually enumerate over the results.

Use something like var results = sort.ToArray() to execute the query, then you will get more accurate results.

Intersection of two string array (ignore case)

7 votes
string[] array1 = {"Red","blue", "green","black"};
string[] array2 = {"BlUe","yellow", "black"};

I need only matching strings in array(Ignore case)

Result should be

**string[] result  = {"blue","black"} or {"BlUe", "black"};**

How about an Enumerable.Intersect and StringComparer combo:

// other options include StringComparer.CurrentCultureIgnoreCase
// or StringComparer.InvariantCultureIgnoreCase
var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase);