开发者

problem in using Distinct with Linq

Hi have an issue with Linq. I have an array of double values with duplicate entri开发者_JAVA技巧es. I want to extract only distinct values from it. I have the following code which doesn't work correctly.

double[] dIds = GetIds(); //dIds has more than 10,000 items

var itemIdCollection = from id in dIds.Distinct()
                   select id;

Console.WriteLine(itemIdCollection.count().ToString());  //count is just 2 !!!!

Can you please give me a solution on this?

Thank you,


First off, you don't have to do that freaky select. Just call dIds.Distinct(). Second, I can guarantee you that it works on any array of doubles. Your doubles are NOT different from everybody else's doubles.

Obviously, if Distinct() is returning an enumerable of a count of 2 (btw, Console.WriteLine(itemIdCollection.Count()) is sufficient) it is because GetIds() returns an array containing only two distinct doubles.

Your assumptions, they are incorrect.


Try this:

List<double> x = new List<double>()
{
    3, 4, 5, 6, 7, 7, 7, 8, 8
};

List<double> distinct = x.Distinct().ToList();

distinct.ForEach(y => Console.WriteLine(y));
Console.ReadKey();


Ghost debugging attempt:

Are you generating new random IDs in your GetIds() function? If so, remember you should be instantiating Random outside the function...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜