-1

I have to exclude items whose ids exist in another list.

List<Int64> Listofid;
var filteredlist = curProjArea.Project.ForEach(x => {x.ProjectId = (where 
 project id does not exist in Listofid) });

Is this possible?

Stefan Steinegger
  • 62,367
  • 15
  • 124
  • 190
Newboy11
  • 2,152
  • 3
  • 21
  • 32

5 Answers5

2

You can filter projects in Where clause:

List<Int64> Listofid;
var filteredlist = curProjArea.Project.Where(x => !Listofid.Contains(x.ProjectId));
Dmitriy Snitko
  • 901
  • 5
  • 14
0
List<Int64> Listofid;
var filteredlist = curProjArea.Project.Where(x =>  !Listofid.Contains(x.ProjectId)).ToList();

Try this out once

Naruto
  • 633
  • 8
  • 18
0
int[] values1 = { 1, 2, 3, 4 };

// Contains three values (1 and 2 also found in values1).
int[] values2 = { 1, 2, 5 };

// Remove all values2 from values1.
var result = values1.Except(values2);

// Show.
foreach (var element in result)
{
    Console.WriteLine(element);
}

From: https://www.dotnetperls.com/except

Han
  • 3,022
  • 2
  • 20
  • 30
0

I think , It's useful for you

List<Int64> Listofid = new List<Int64>() { 5, 3, 9, 7, 5, 9, 3, 7 };
List<Int64> filteredlist = new List<Int64>()  { 8, 3, 6, 4, 4, 9, 1, 0 };
List<Int64> Except = filteredlist.Except(Listofid).ToList();
Console.WriteLine("Except Result");
foreach (int num in Except)
{
    Console.WriteLine("{0} ", num); //Result =  8,6,4,1,0
}
0

Just negate the .Contains on the list for which you want to exclude it.

var filteredList = curProjArea.Project.Where(a => !Listofid.Contains(a.ProjectId));

Demo in Dotnet fiddle

Karthik Chintala
  • 5,407
  • 5
  • 28
  • 59