在另一个列表中不存在ID的Lambda

时间:2018-10-08 05:38:20

标签: c# lambda

我必须排除ID在另一个列表中的项目。

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

这可能吗?

5 个答案:

答案 0 :(得分:2)

您可以在Where子句中过滤项目:

List<Int64> Listofid;
var filteredlist = curProjArea.Project.Where(x => !Listofid.Contains(x.ProjectId));

答案 1 :(得分:0)

List<Int64> Listofid;
var filteredlist = curProjArea.Project.Where(x =>  !Listofid.Contains(x.ProjectId)).ToList();

尝试一次

答案 2 :(得分: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);
}

发件人: https://www.dotnetperls.com/except

答案 3 :(得分:0)

我认为,这对您有用

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
}

答案 4 :(得分:0)

只需取消要排除其列表上的.Contains

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

Demo in Dotnet fiddle