C# 比较两个字典并返回缺少的值
如何比较这两个字典并只返回缺少的值 GetFileListFromBlob函数获取所有文件名,我想知道数据库中缺少什么 还是有更好的方法从这些对象中获取缺少的值?我应该使用不同的键/值吗C# 比较两个字典并返回缺少的值,c#,dictionary,.net-core,C#,Dictionary,.net Core,如何比较这两个字典并只返回缺少的值 GetFileListFromBlob函数获取所有文件名,我想知道数据库中缺少什么 还是有更好的方法从这些对象中获取缺少的值?我应该使用不同的键/值吗 Dictionary<int, string> databaseFileList = new Dictionary<int, string>; Dictionary<int, string> blobFileList = new Dictionary<int, stri
Dictionary<int, string> databaseFileList = new Dictionary<int, string>;
Dictionary<int, string> blobFileList = new Dictionary<int, string>;
int counter = 0;
foreach (string f in GetFileListFromDB())
{
counter++;
databaseFileList.Add(counter, f );
}
counter = 0;
foreach (string f in GetFileListFromBlob())
{
counter++;
blobFileList.Add(counter, f);
}
// How to compare?
谢谢你在我看来,你一开始不需要计数器,以后可以添加 您可以使用System.Collections.Generic.List类型继续
List<int, string> databaseFileList = new List<string>(GetFileListFromDB());
List<int, string> blobFileList = new List<string>(GetFileListFromBlob());
//some code
现在使用除。。。用于比较集合的方法:
var missingItems1 = allItems .Except(databaseFileList);
//or
var missingItems1 = allItems .Except(blobFileList);
//or
//some other code
在我看来,一开始你不需要计数器,以后你可以添加它们 您可以使用System.Collections.Generic.List类型继续
List<int, string> databaseFileList = new List<string>(GetFileListFromDB());
List<int, string> blobFileList = new List<string>(GetFileListFromBlob());
//some code
现在使用除。。。用于比较集合的方法:
var missingItems1 = allItems .Except(databaseFileList);
//or
var missingItems1 = allItems .Except(blobFileList);
//or
//some other code
A可能是您想要的,而不是字典-例如:
var reference=newhashset{a,b,c,d};
var比较=新哈希集{a,d,e};
现在在引用集上调用ExceptWith时
参考。比较除外;
。。。引用集将包含比较集中不存在的元素b和c。然而,请注意,额外的元素e并没有被捕获,而是通过交换集合来获取e作为缺少的元素,并且该操作会在适当的位置修改引用集合。如果不希望这样做,那么LINQ操作符可能值得研究,正如在另一个答案中已经提到的那样。A可能是您想要的,而不是字典-举个例子:
var reference=newhashset{a,b,c,d};
var比较=新哈希集{a,d,e};
现在在引用集上调用ExceptWith时
参考。比较除外;
。。。引用集将包含比较集中不存在的元素b和c。然而,请注意,额外的元素e并没有被捕获,而是通过交换集合来获取e作为缺少的元素,并且该操作会在适当的位置修改引用集合。如果不希望这样,那么LINQ操作符可能值得研究,正如在另一个答案中已经提到的那样。
私人无效比较法院
{
var databaseFileList=新字典;
var blobFileList=新字典;
databaseFileList.Add300,苹果;
databaseFileList.Add500,windows;
databaseFileList.Add100,比尔;
blobFileList.Add100,比尔;
blobFileList.Add200,史蒂夫;
var result=databaseFileList.Whered2=>!blobFileList.Anyd1=>d1.Key==d2.Key.ToList;
}
私人无效比较法院
{
var databaseFileList=新字典;
var blobFileList=新字典;
databaseFileList.Add300,苹果;
databaseFileList.Add500,windows;
databaseFileList.Add100,比尔;
blobFileList.Add100,比尔;
blobFileList.Add200,史蒂夫;
var result=databaseFileList.Whered2=>!blobFileList.Anyd1=>d1.Key==d2.Key.ToList;
}改用HashSet并去掉计数器,用var-databaseFileSet=new-HashSetGetFileListFromDB、StringComparer.InvariantCultureIgnoreCase构建数据库HashSet,然后只执行var-missing=GetFileListFromBlob.Wheref=>!databaseFileSet.Containsf.ToList;。您正在查找第一个列表或第二个列表中缺少的值吗?请改用HashSet并去掉计数器,使用var-databaseFileSet=new-HashSetGetFileListFromDB、StringComparer.InvariantCultureIgnoreCase构建数据库HashSet,然后只需执行var-missing=GetFileListFromBlob.Wheref=>!databaseFileSet.Containsf.ToList;。您正在查找第一个列表或第二个列表中缺少的值吗?与任何方法相比,字典的ContainsValue方法可能是更好的选择。对于复杂数据类型,字典的ContainsValue方法可能是更好的选择,您必须以有意义的方式实现IEquatable并重写GetHashCode。@对于复杂的数据类型,您必须以有意义的方式实现IEquatable并重写GetHashCode。