-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
  • 63,782
  • 15
  • 129
  • 193
Newboy11
  • 2,778
  • 5
  • 26
  • 40

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
  • 931
  • 5
  • 14
0
List<Int64> Listofid;
var filteredlist = curProjArea.Project.Where(x =>  !Listofid.Contains(x.ProjectId)).ToList();

Try this out once

Naruto
  • 653
  • 8
  • 19
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,052
  • 2
  • 22
  • 31
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,465
  • 5
  • 30
  • 60