0

I have a list of numbers:

List<int> lstActive = new List<int>{1,6,7,8,10}; 

I want to get the numbers that does not exit in the above list and less than 10 e.g.

private List<int> GetInactive(List<int> lstActive, int MaxValue)
{
//To Do
}

Then:

List<int> lstInactive = GetInactive(lstActive, 10)

the result should be: {2,3,4,5,9}

How can I do this ?

ppau2004
  • 193
  • 2
  • 3
  • 16
  • Take a look at this post: https://stackoverflow.com/questions/3944803/use-linq-to-get-items-in-one-list-that-are-not-in-another-list – David Tansey Feb 20 '19 at 23:12
  • Also, you don't have to pass an array to the `List` constructor. You could built it like so: `List lstActive = new List { 1, 6, 7, 8, 10 };` – Robert Stefanic Feb 20 '19 at 23:16

2 Answers2

2
Enumerable.Range(0, maxValue).Where(n => !lstActive.Contains(n))

If perf is an issue, make a hashset:

var hs = new HashSet<int>(lstActive);
Enumerable.Range(0, maxValue).Where(n => !hs.Contains(n))
Sava B.
  • 1,007
  • 1
  • 10
  • 21
1

Try This:

List<int> lstActive = new List<int>(new int[]{1,6,7,8,10}); 
Enumerable.Range(1, 10).ToList().Except(lstActive).Dump();

https://dotnetfiddle.net/hyiAhs

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72