Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ使用where子句在c中循环通过不同的数组#_C#_Linq - Fatal编程技术网

C# LINQ使用where子句在c中循环通过不同的数组#

C# LINQ使用where子句在c中循环通过不同的数组#,c#,linq,C#,Linq,我想返回报告中不存在的姓名列表。但是,我不知道如何使用LINQ正确循环IEnumerable name。是否有一种使用LINQ循环另一个数组的方法 private class Report { public string UserName { get; set; } public string city { get; set; } public string image { get; set; }

我想返回
报告
中不存在的姓名列表。但是,我不知道如何使用LINQ正确循环
IEnumerable name
。是否有一种使用LINQ循环另一个数组的方法

    private class Report
        {
            public string UserName { get; set; }
            public string city { get; set; }
            public string image { get; set; }
        }


  List<Report>report = await _service(id).ConfigureAwait(false);
  IEnumerable<string> names = await _names(id).ConfigureAwait(false);

// only want to get list of names that do not exist in report

 var newList = reports.Where(x => x.UserName.Where(i => != names)); // doesn't work
私有类报告
{
公共字符串用户名{get;set;}
公共字符串city{get;set;}
公共字符串图像{get;set;}
}
Listreport=await\u服务(id).ConfigureAwait(false);
IEnumerable names=await\u names(id).ConfigureAwait(false);
//只想获取报表中不存在的名称列表
var newList=reports.Where(x=>x.UserName.Where(i=>!=names));//不起作用

您可以使用Contains方法。试试看:

var newList = reports.Where(x => !names.Contains(x.UserName)));

只想获取报告中不存在的名称列表

然后,您需要查询
名称
,以获取不存在的名称

var newList = names.Where(x => !reports.Any(xx => xx.UserName == x));
试试这个:

 var newList = names.Where(n => reports.Any(r => r.UserName != n));

我会将名称构造为哈希集或字典,以防止O(n^2)。您可能还希望添加字符串.equals(xx,xx.UserName,StringComparison.OrdinalIgnoreCase)检查,而不是直接的equals