Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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查询中使用字典inside Contains方法_C#_Linq - Fatal编程技术网

C# 在linq查询中使用字典inside Contains方法

C# 在linq查询中使用字典inside Contains方法,c#,linq,C#,Linq,我正在尝试选择字典中有映射的项 这就是我想做的 var columnsMapping = (from dc in sourceTable.Columns.Cast<DataColumn>() where dc.ColumnName.Contains(columnsMappingDictionary.Keys.GetEnumerator()) select new

我正在尝试选择字典中有映射的项

这就是我想做的

    var columnsMapping = (from dc in sourceTable.Columns.Cast<DataColumn>()
                          where dc.ColumnName.Contains(columnsMappingDictionary.Keys.GetEnumerator())
                          select new
                          {
                              columnName = dc.ColumnName,
                              columnType = dc.DataType
                          }).ToDictionary(key => key.columnName, value => value.columnType);
我的字典是string,string类型的

我一直在犯这个错误

The best overloaded method match for 'string.Contains(string)' has some invalid arguments

cannot convert from 'System.Collections.Generic.Dictionary<string,string>.KeyCollection.Enumerator' to 'string'
有人能帮我更正我的代码吗。

反之亦然: 其中columnsMappingDictionary.Keys.Containsdc.ColumnName

反之亦然:
where columnsMappingDictionary.Keys.Containsdc.ColumnName

不清楚您对where条件的理解是什么。我认为有两种可能性:

您想应用dc.ColumnName.Contains。。。字典中的每个键,或 要根据现有字典键检查dc.ColumnName, 我在下面讨论这两种情况

以下是如何将字符串与一组键进行匹配:

where columnsMappingDictionary.Keys.Any(key => dc.ColumnName.Contains(key))
当dc.ColumnName将任何键作为其子字符串时,此条件为真

以下是如何检查columnsMappingDictionary的键中是否存在dc.ColumnName:


现在还不清楚你对where条件的理解是什么。我认为有两种可能性:

您想应用dc.ColumnName.Contains。。。字典中的每个键,或 要根据现有字典键检查dc.ColumnName, 我在下面讨论这两种情况

以下是如何将字符串与一组键进行匹配:

where columnsMappingDictionary.Keys.Any(key => dc.ColumnName.Contains(key))
当dc.ColumnName将任何键作为其子字符串时,此条件为真

以下是如何检查columnsMappingDictionary的键中是否存在dc.ColumnName:


正如错误所说,当您应该向.Contains方法传递字符串时,您正在传递枚举数。为什么要使用GetEnumerator?因为它不返回字符串,所以我想,因为我的键是字符串,GetEnumerator会给我一个字符串,然后我可以进行比较。就像错误所说的,当你应该将字符串传递给.Contains方法时,你正在传递枚举数。为什么要使用GetEnumerator?因为它不返回字符串,所以我想,因为我的键是字符串,GetEnumerator会给我一个字符串,然后我可以进行比较。