nnmmss
nnmmss

Reputation: 2974

Except method gives me wrong answer just for one instance

I create a list by this command

var terminalList = sourceLists.Where(t => t.TagNo == tagList)
                              .Where(t => t.FromTerminal.Length > 0)
                              .Select(t => parseTerminal(t.FromTerminal))
                              .OrderBy(t => t).ToList();

It works fine for every Tagno except for one which create a list like this

terminalList={33,35}

When I use this command

var result = Enumerable.Range(terminalList.Min(), terminalList.Max())
                       .Except(terminalList)
                       .ToList();

I checked it. terminalList.Min() would be 33 and terminalList.Max() would be 35. and in this case terminalList has 2 items in it.

I get this answer:

result = {34,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67}

which is wrong the correct answer is {34}.

How can I trace the problem?

Upvotes: 0

Views: 95

Answers (2)

Jurgen Camilleri
Jurgen Camilleri

Reputation: 3589

Try changing this:

var result = Enumerable.Range(terminalList.Min(), terminalList.Max())
                       .Except(terminalList)
                       .ToList();

to this:

var result = Enumerable.Range(terminalList.Min(), terminalList.Max() - terminalList.Min())
                       .Except(terminalList)
                       .ToList();

As Guru Stron said you are using Enumerable.Range() incorrectly.

Upvotes: 0

Guru Stron
Guru Stron

Reputation: 141665

public static IEnumerable<int> Range(
    int start,
    int count
)

where

start - The value of the first integer in the sequence. count - The number of sequential integers to generate.

so Enumerable.Range(33,35) generates you 35 elements starting from 33, an then you remove 33 and 35 and get the shown result

Upvotes: 7

Related Questions