Eps <ep*@mailinator .comwrote:
I am doing the following, this is a List of audio files.
this.Where(p =p.Album == AnAudioFileObje ct.Album).Selec t(s =>
s.Artist).Disti nct().Count() 1;
The aim is to determine whether AnAudioFileObje ct is from an album that
has various artists on it or just one artist.
If I load several thousand audio files into the list it becomes very
slow, can anyone think of a way I could speed this up ?.
I've just thought of something which could theoretically help. Instead
of calling Count(), call Take(1).Any(). That way as soon as Distinct()
yields its second element, you can finish.
That way you can deal with *huge* sequences, or even infinite ones (so
long as they aren't an infinite sequence repeating a single element).
For example:
using System;
using System.Linq;
public static class Test
{
static void Main()
{
var allPositiveInts = Enumerable.Rang e(0, int.MaxValue);
bool quick = allPositiveInts .Distinct().Tak e(1).Any();
Console.WriteLi ne("quick = " + quick);
bool slow = allPositiveInts .Distinct().Cou nt() 1;
Console.WriteLi ne ("slow = " + slow);
}
}
The result is:
quick = True
Unhandled Exception: OutOfMemoryExce ption.
Having said all of this, I strongly suspect that you won't gain much
from this. Try printing out
this.Where(p =p.Album == AnAudioFileObje ct.Album)
.Select(s =s.Artist)
.Count()
just to see how many artist entries we're talking about as the input to
Distinct() to start with.
--
Jon Skeet - <sk***@pobox.co m>
Web site:
http://www.pobox.com/~skeet
Blog:
http://www.msmvps.com/jon.skeet
C# in Depth:
http://csharpindepth.com