Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#根据主列表扫描列表以查找缺少的项目_C#_List_Compare - Fatal编程技术网

C#根据主列表扫描列表以查找缺少的项目

C#根据主列表扫描列表以查找缺少的项目,c#,list,compare,C#,List,Compare,我有一个主列表,其中包含我知道正确的数据库表的值: masterList: List<string>(){ "business", "customer", "location", "employee", etc} masterList:List() 我查询了一个新的数据库,它应该是相同的。我的测试将告诉我,我的团队为创建这个新数据库而编写的脚本中是否有任何错误。tableList应该是我的查询的返回: tablesList: List<string>(){ "busi

我有一个主列表,其中包含我知道正确的数据库表的值:

masterList: List<string>(){ "business", "customer", "location", "employee", etc}
masterList:List()
我查询了一个新的数据库,它应该是相同的。我的测试将告诉我,我的团队为创建这个新数据库而编写的脚本中是否有任何错误。tableList应该是我的查询的返回:

tablesList: List<string>(){ "business", "customer", "location", "employee", etc}
tableList:List(){“业务”、“客户”、“位置”、“员工”等}
所以在实践中,它们应该是相同的,但是为了测试错误,我想将表列表与主列表进行比较,以确保所有需要的表都存在。作为这个过程的一个副本,我也在反向搜索,以防主列表中没有任何额外的表

问题:如何将列表与主列表进行比较,并返回不匹配的项目?

我正在使用Visual Studio 2017和c#.net Core 2.0

以下是我到目前为止一直在尝试的:

var errorsList = new List<string>();
tablesList = QuerySchemaForTables();
masterList = GrabMasterTableList();
foreach(var item in masterList)
                    errorsList.Add(tablesList.Where(x => x.Contains(item)));
var errorsList=new List();
tablesList=QuerySchemaForTables();
主列表=GrabMasterTableList();
foreach(主列表中的变量项)
errorsList.Add(tableList.Where(x=>x.Contains(item));
但是有了这个,我得到了一个错误:

无法从IEnumerable转换为字符串


你在找这样的东西吗

var errorList = tableList.Where(x => !masterList.Contains(x));

您的代码当前正在尝试将IEnumerable添加到列表中

如果要添加所有匹配项,则应改为添加范围

var errorsList=new List();
tablesList=QuerySchemaForTables();
主列表=GrabMasterTableList();
foreach(主列表中的变量项)
errorsList.AddRange(tableList.Where(x=>x.Contains(item));

要查找
表列表中但不在
主列表中的所有项目,请使用
.Contains

var errorsList = tableList.Where(x => !masterList.Contains(x));
但我建议您对主列表使用
HashSet
,因此在其中搜索项目将在
O(1)
中,而不是
O(n)

var masterCollection=newhashset(GrabMasterTableList());
var errorsList=tableList.Where(x=>!masterCollection.Contains(x));

关于您发布的代码的问题:

foreach(var item in masterList)
    errorsList.Add(tablesList.Where(x => x.Contains(item))); // <-- error
foreach(主列表中的变量项)

errorsList.Add(tableList.Where(x=>x.Contains(item));// 您可以使用LINQ获得错误的两个方向。不需要循环:

var missingInMasterList = tableList.Where(x => !masterList.Contains(x)).ToList();
var missingInTableList = masterList.Where(x => !tableList.Contains(x)).ToList();

我认为最好使用的是
除了()
,如下所示

var MasterList =  new List<string> { "business", "customer", "location", "employee"};
var ChildList = new List<String> {  "customer", "location", "employee" };

var filter = MasterList.Except(ChildList);
var MasterList=新列表{“业务”、“客户”、“位置”、“员工”};
var ChildList=新列表{“客户”、“位置”、“员工”};
变量过滤器=主列表。除(子列表)外;

这将显示那些不在
ChildList
中的值。您也可以反之亦然。

您可以使用
捕获差异。除了()
,它是
IEnumerable
中的一个:

然后,要创建错误消息,您可以使用
string.join()
,用逗号将这些
IEnumerables
中的项目连接到一个字符串中:

然后,您可以通过检查
errorMessage
的长度来输出结果,以确定是否遇到任何错误:

if (errorMessage.Length > 0)
{
    Console.WriteLine(errorMessage.ToString());
}
else
{
    Console.WriteLine("No extra or missing tables detected");
} 

这不是你的错误。请告诉我们错误的完整文本。参数1:无法将问题中的“System.Collections.Generic.IEnumerable”转换为“string”。我已经知道错误消息是什么。你需要告诉那些没有将你的代码粘贴到IDE中的人。在
Where(x=>x.Contains(item))
x
是什么?我已经知道了。看来
x
GrabMasterTableList
的返回值,它似乎显示为
TableList:List(){“业务”、“客户”、“位置”、“员工”等}
给我和编译器,似乎
x
tableList
中的字符串之一。不要使用
List
作为变量名称。这令人困惑。如果你真的想,你可以使用
list
,但最好使用一个更有意义的名字。同意并更新。谢谢你记下来。这已经产生了我一直在寻找的结果。当我测试主列表时,我知道该列表缺少项,并且有主列表没有的额外项,这为我提供了一种输出它们的方法。
var MasterList =  new List<string> { "business", "customer", "location", "employee"};
var ChildList = new List<String> {  "customer", "location", "employee" };

var filter = MasterList.Except(ChildList);
var missingTables = masterList.Except(tablesList);
var extraTables = tablesList.Except(masterList);
var errorMessage = new StringBuilder();

if (missingTables.Any())
{
    errorMessage.AppendLine("Missing Tables: " + string.Join(", ", missingTables));
}

if (extraTables.Any())
{
    errorMessage.AppendLine("Extra Tables: " + string.Join(", ", extraTables));
}
if (errorMessage.Length > 0)
{
    Console.WriteLine(errorMessage.ToString());
}
else
{
    Console.WriteLine("No extra or missing tables detected");
}