Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 如何检索重复的键值对? 公共静态IEnumerable GetGroupKeyValuePairs(字符串类别) { var list=新列表(); 使用(DataConnection=newdataconnection()) { 列表设置=connection.Get() .其中(a=>a.类别==类别) .Select(pair=>newkeyvaluepair(pair.TheName,pair.TheValue)) .ToList(); 列表=设置; } 退货清单; }_C# - Fatal编程技术网

C# 如何检索重复的键值对? 公共静态IEnumerable GetGroupKeyValuePairs(字符串类别) { var list=新列表(); 使用(DataConnection=newdataconnection()) { 列表设置=connection.Get() .其中(a=>a.类别==类别) .Select(pair=>newkeyvaluepair(pair.TheName,pair.TheValue)) .ToList(); 列表=设置; } 退货清单; }

C# 如何检索重复的键值对? 公共静态IEnumerable GetGroupKeyValuePairs(字符串类别) { var list=新列表(); 使用(DataConnection=newdataconnection()) { 列表设置=connection.Get() .其中(a=>a.类别==类别) .Select(pair=>newkeyvaluepair(pair.TheName,pair.TheValue)) .ToList(); 列表=设置; } 退货清单; },c#,C#,例外情况是: 无效操作例外: “Garanti.Oda”键出现多次 如何收集重复的键?假设您只想通过键查找重复的键(例如,这样您就可以建立一个字典),您可以GroupBy预期键并查找多个键的所有实例: public static IEnumerable<KeyValuePair<string, string>> GetGroupKeyValuePairs(string category) { var list = new List<K

例外情况是:

无效操作例外: “Garanti.Oda”键出现多次


如何收集重复的键?

假设您只想通过
查找重复的键(例如,这样您就可以建立一个字典),您可以
GroupBy
预期键并查找多个键的所有实例:

    public static IEnumerable<KeyValuePair<string, string>> GetGroupKeyValuePairs(string category)
    {
        var list = new List<KeyValuePair<string, string>>();

        using (DataConnection connection = new DataConnection())
        {
            List<KeyValuePair<string,string>> settings = connection.Get<Settings>()
                .Where(a => a.Category == category )
                .Select(pair => new KeyValuePair<string,string>(pair.TheName, pair.TheValue))
                .ToList();

            list = settings;

        }

        return list;
    }
var dupeSettings=connection.Get()
.其中(a=>a.类别==类别)
.GroupBy(a=>a.TheName)
.Where(grp=>grp.Count()>1)
.Select(dupe=>dupe.Key)
.ToList();
或者,如果您希望通过匿名类复制密钥和值、项目和组:

 var dupeSettings = connection.Get<Settings>()
            .Where(a => a.Category == category)
            .GroupBy(a => a.TheName)
            .Where(grp => grp.Count() > 1)
            .Select(dupe => dupe.Key)
            .ToList();
var dupeSettings=connection.Get()
.其中(a=>a.类别==类别)
.GroupBy(a=>new{a.TheName,a.TheValue})
.Where(grp=>grp.Count()>1)
.Select(dupe=>dupe.Key)//Key.TheName,Key.TheValue
.ToList();

您展示的方法不会对具有相同密钥的多个对产生问题。我假设之后,你正在做一些类似于创建这些对的字典的事情,这就是你遇到问题的地方。例如

 var dupeSettings = connection.Get<Settings>()
            .Where(a => a.Category == category)
            .GroupBy(a => new {a.TheName, a.TheValue})
            .Where(grp => grp.Count() > 1)
            .Select(dupe => dupe.Key) // Key.TheName, Key.TheValue
            .ToList();

然后,例如,如果列表中有
“a”、“b”
“a”、“c”
,那么
查找[“a”]
将为您提供
“b”
“c”

@mybirthname:OP应该能够创建任意多个
KeyValuePair
实例,并使用它们喜欢的密钥,他们应该能够在一个简单的
列表中添加任意数量的代码。该结构没有任何固有特性可以阻止同一密钥同时存在于多个
KeyValuePair
结构中。@O.R.Mapper我将阅读有关密钥、值对的内容,以检查您编写的内容,我使用它们2-3次可能是我的错误。谢谢@我的出生名:@OrelEraki:你怎么知道他们“不能”呢?另外,您指的是什么
NameValuePair
类型?从哪里来的?还是从我这里来的?所有这些听起来都不应该也不可能从第三方通用代码中使用。@O.R.Mapper绝对正确。。然后我使用List.Distinct()方法删除重复项。
var pairs = GetGroupKeyValuePairs("some category");
var dict = new Dictionary<string, string>();
foreach (var pair in pairs)
    dict.Add(pair.Key, pair.Value); // exception when it hits a duplicate
var pairs = GetGroupKeyValuePairs("some category");
var lookup = pairs.ToLookup(x => x.Key, x => x.Value);