Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Trying to avoid duplicates

Tags:

c#

linq

var multiples = from i in Enumerable.Range(min, (max - min))
                        from r in roots
                        where i % r == 0
                        select i;

For example, if roots = {2,10} it would select 20 twice.

Is it possible to avoid duplicates here?

like image 977
user1376243 Avatar asked Mar 08 '13 06:03

user1376243


2 Answers

Use the Distinct

var multiples = (from i in Enumerable.Range(min, (max - min))
                 from r in roots
                 where i % r == 0
                 select i).Distinct();

This works well on simple types like string and int. not very well on anonymous types.

In your case the i is an int, and it should therefore be ably to sort out dublicates.

EDIT

It does infact works on anonymous types (see Jeppe's comment). because as @Jeppe said the anonymous types have a "good" Equals enabeling the Distict to determin if the to objects are equal / dublicates.

like image 132
Jens Kloster Avatar answered Oct 02 '22 23:10

Jens Kloster


You can use Any() instead of a full Cartesian join:

var multiples = from i in Enumerable.Range(min, (max - min))
                where roots.Any(r => i % r == 0)
                select i;

This has the added advantage that it will stop testing elements in roots as soon as it finds one that succeeds, and it does not require a second pass through to pull out the distinct elements.

like image 27
lc. Avatar answered Oct 02 '22 23:10

lc.