Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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从FormCollection进行LINQ查询/投影_C#_Linq - Fatal编程技术网

C# C从FormCollection进行LINQ查询/投影

C# C从FormCollection进行LINQ查询/投影,c#,linq,C#,Linq,我有一个动态表单,不能使用MVC绑定。当我将此FormCollection发布到我的控制器simplified时,表单集合中有以下数据: public ActionResult Foo(FormCollection coll) ... coll["Data0Key"] contains "category" coll["Data0Value"] contains "123" coll["Data1Key"] contains "location" coll["Data1Va

我有一个动态表单,不能使用MVC绑定。当我将此FormCollection发布到我的控制器simplified时,表单集合中有以下数据:

public ActionResult Foo(FormCollection coll)
...
  coll["Data0Key"] contains "category" 
  coll["Data0Value"] contains "123" 
  coll["Data1Key"] contains "location" 
  coll["Data1Value"] contains "21" 
  coll["Data7Key"] contains  "area"
  coll["Data7Value"] contains "test"
  coll["SomethingElse"] contains "irrelevent"
。。我有一个未知数量的这些,并希望创建从集合中的单独键和值对象的键值对

我一直在沿着这条路线努力

    var settings = coll.AllKeys
        .Where(k => k.StartsWith("Data"))
        .ToDictionary(k => k, k => coll[k]);
这给了我一本字典:

Data0Key, category
Data0Value, 123 
Data1Key, location 
Data1Value, 21
Data7Key, area
Data7Value, test
我真正想要的是一个键值对的集合,其结构如下

 category, 123
 location, 21
 area, test

我试图实现的目标是可能的,还是我需要找到一种不同的方法

我认为您只需要迭代DataxKey部分,然后查找值。比如:

其中k=>k.StartsWithData和k.EndsWithKey .ToDictionaryk=>coll[k],k=>coll[k.ReplaceKey,Value];
这假设每个Data0Key也有一个Data0Value匹配对,否则它将取消引用coll中不存在的密钥。

我认为您只想迭代DataxKey部分,然后查找值。比如:

其中k=>k.StartsWithData和k.EndsWithKey .ToDictionaryk=>coll[k],k=>coll[k.ReplaceKey,Value];
这假设每个Data0Key也有一个Data0Value匹配对,否则它将取消引用coll中不存在的密钥。

这可以通过相对简单的LINQ查询完成:

var data = new Dictionary<string,object> {
     ["Data0Key"] = "category" 
,    ["Data0Value"] = "123" 
,    ["Data1Key"] = "location" 
,    ["Data1Value"] = "21" 
,    ["Data7Key"] =  "area"
,    ["Data7Value"] = "test"
,    ["SomethingElse"] = "irrelevent"                 
};
var kvp = data
    .Where(p => p.Key.StartsWith("Data") && (p.Key.EndsWith("Key") || p.Key.EndsWith("Value")))
    .Select(p => new {
        Key = p.Key.Substring(0, p.Key.Length - (p.Key.EndsWith("Key") ? 3 : 5))
    ,   IsKey = p.Key.EndsWith("Key")
    ,   p.Value
    })
    .GroupBy(p => p.Key)
    .Where(g => g.Count(p => p.IsKey) == 1 && g.Count(p => !p.IsKey) == 1)
    .ToDictionary(g => (string)g.Single(p => p.IsKey).Value, g => g.Single(p => !p.IsKey).Value);
foreach (var p in kvp) {
    Console.WriteLine("{0} - {1}", p.Key, p.Value);
}
下面逐行解释所做的工作:

首先,通过确保只保留数据前缀,并且后缀是键或值,过滤掉不相关的项 然后,通过移除键或值后缀来提取组键;值与IsKey标志一起添加到列表中,IsKey标志指示项是键还是值 项目按组键Data0、Data7等分组。 检查每个组是否只包含一个键和一个值;不完整的组将被丢弃 最后,将组转换为键值对字典。
这可以通过一个相对简单的LINQ查询完成:

var data = new Dictionary<string,object> {
     ["Data0Key"] = "category" 
,    ["Data0Value"] = "123" 
,    ["Data1Key"] = "location" 
,    ["Data1Value"] = "21" 
,    ["Data7Key"] =  "area"
,    ["Data7Value"] = "test"
,    ["SomethingElse"] = "irrelevent"                 
};
var kvp = data
    .Where(p => p.Key.StartsWith("Data") && (p.Key.EndsWith("Key") || p.Key.EndsWith("Value")))
    .Select(p => new {
        Key = p.Key.Substring(0, p.Key.Length - (p.Key.EndsWith("Key") ? 3 : 5))
    ,   IsKey = p.Key.EndsWith("Key")
    ,   p.Value
    })
    .GroupBy(p => p.Key)
    .Where(g => g.Count(p => p.IsKey) == 1 && g.Count(p => !p.IsKey) == 1)
    .ToDictionary(g => (string)g.Single(p => p.IsKey).Value, g => g.Single(p => !p.IsKey).Value);
foreach (var p in kvp) {
    Console.WriteLine("{0} - {1}", p.Key, p.Value);
}
下面逐行解释所做的工作:

首先,通过确保只保留数据前缀,并且后缀是键或值,过滤掉不相关的项 然后,通过移除键或值后缀来提取组键;值与IsKey标志一起添加到列表中,IsKey标志指示项是键还是值 项目按组键Data0、Data7等分组。 检查每个组是否只包含一个键和一个值;不完整的组将被丢弃 最后,将组转换为键值对字典。
字典与键值对集合有何区别?请原谅我的术语,我对字典很在行,这是我正在努力实现的结构。我刚刚又读了一遍你的问题,我现在明白了你的意思:字典与键值对集合有何区别?请原谅我的术语,我对字典很在行,这是我正在努力实现的结构。我刚刚又读了一遍你的问题,现在我明白你的意思了: