我遍历KeyValuePair的集合,然后将Key和Value复制到一个新创建的类中,如下所示:
Classes.MemberHierarchy membHier = new Classes.MemberHierarchy();
List<Classes.MemberHierarchy> membHierList = new List<Classes.MemberHierarchy>();
foreach (KeyValuePair<string, string[]> acct in acctData)
{
membHier.entityName = acct.Key;
membHier.Accounts = acct.Value;
membHierList.Add(membHier);
}
Run Code Online (Sandbox Code Playgroud)
问题在于,在第二次迭代中,membHierList属性立即被第一次迭代中的值覆盖。真奇怪
因此,在第一次迭代时,membHier.entityName是“ ABC Member”,并且使用字符串数组填充Accounts没问题。
然后在第二次迭代中,membHier.entityName为“ XYZ成员”。
现在,“ XYZ成员”占据了两个插槽,如下所示
membHierList [0] .base.entityName =“ XYZ成员” membHierList [1] .base.entityName =“ XYZ成员”
我上方有物体冲突吗?
预先谢谢你。
我有
Dictionary<String, List<String>> filters = new Dictionary<String, List<String>>();
Run Code Online (Sandbox Code Playgroud)
这是有价值的country = us.到目前为止,我可以在不重复按键时添加它.现在当country重复密钥时.它显示密钥已经存在.
我想要的是如何在同一个键中添加多个值.我无法做到.请提出建议.
for (int i = 0; i < msgProperty.Value.Count; i++)
{
FilterValue.Add(msgProperty.Value[i].filterValue.Value);
filterColumn = msgProperty.Value[i].filterColumnName.Value;
filters.Add(filterColumn, FilterValue);
}
Run Code Online (Sandbox Code Playgroud)
我想要的是
country = US,UK
如果我们使用结构或类作为键,则需要比较函数将值放在树中,但如果将一对用作键,那么地图数据结构如何将值放在树中.即必须有一些东西来比较密钥并将它们存储在树中.
我想为我的 KeyValuePair 对象分配一些静态值。
private IEnumerable<KeyValuePair<string, string>> getCountries()
{
return new List<KeyValuePair<string, string>>()
{
{ "code1", "value1" },
{ "code2", "value2" }
};
}
Run Code Online (Sandbox Code Playgroud)
但这会引发 nooverloaded 方法错误。
我的两个ForEach循环允许访问errorOrders(用户名和错误数)和totalOrders(用户名和他们的总订单数).
我的代码不断循环遍历这两个ForEaches.同时在"计数" errorOrders,并totalOrders为38和计划,通过所有38个用户就好循环.但是它一次又一次地循环遍历它们,重新完成它刚刚完成的过程.
我怎样才能在用户中循环一次?
foreach (KeyValuePair<string, int> error in errorOrders)
{
foreach (KeyValuePair<string, int> total in totalOrders)
{
errPercentage = ((double)error.Value / (double)total.Value);
Console.WriteLine("Percentage of errors for " + total.Key + ": " + Math.Round(errPercentage, 2) * 100 + "%");
ordersPerHour = OrdersPerHour(total.Key);
RandomOrders = RandomSelect(errPercentage, total.Key);
Console.WriteLine("Number of orders pulled : " + RandomOrders.Rows.Count);
//Print out orders randomly collected
for (int i = 0; i < RandomOrders.Rows.Count; i++)
{
Console.WriteLine(RandomOrders.Rows[i]["ControlNumber"]);
}
Console.WriteLine("\r\n"); …Run Code Online (Sandbox Code Playgroud) 我需要将一个整数数组转换为KeyValuePair列表,其中字符串可以是一个空字符串.这样做有效而优雅的方法是什么?
所以从这个:
int[] ints = new int[] { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
对此:
List<KeyValuePair<int, string>> pairs = new List<KeyValuePair<int, string>>();
pairs.Add(new KeyValuePair<int, string>(1, ""));
pairs.Add(new KeyValuePair<int, string>(2, ""));
pairs.Add(new KeyValuePair<int, string>(3, ""));
Run Code Online (Sandbox Code Playgroud)
显然有很多方法可以做到这一点,从for循环开始,但我最好寻找一行代码,如果可能的话,也许是一个linq语句.
假设我有两个KeyValuePair变量.
KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>("a", 5);
KeyValuePair<string, double> kv2 = new KeyValuePair<string, double>("b", 7);
Run Code Online (Sandbox Code Playgroud)
"KeyValuePair"结构中没有+ operation的定义.
所以我想使用运算符重载!
我的目标是获得第三个KeyValuePair类型变量,如:
KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>(kv1.Key + " + " + kv2.Key, kv1.Value + kv2.Value);
Run Code Online (Sandbox Code Playgroud)
结果将是:
KeyValuePair<string, double>("a + b", 12)
Run Code Online (Sandbox Code Playgroud)
但是告诉我如何使用"运营商"来做到这一点?
我试图这样做:
public partial class Form1 : Form
{
public Form1()
{
KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>("a", 5);
KeyValuePair<string, double> kv2 = new KeyValuePair<string, double>("b", 7);
KeyValuePair<string, double> k = kv1 + kv2;
}
public …Run Code Online (Sandbox Code Playgroud) 我想要一个返回新KeyValuePair <T,T>的方法
为什么?因为我想使用像这样的方法
...
GetAsKVP("A", "B"),
GetAsKVP("C", "D"),
...
Run Code Online (Sandbox Code Playgroud)
代替
...
new KeyValuePair<string, string>("A", "B"),
new KeyValuePair<string, string>("C", "D")
...
Run Code Online (Sandbox Code Playgroud)
当我向params KeyValuePair [] pKVP添加值时
它更快,更易读.
我试过了
public static KeyValuePair<T, T> GetAsKVP(T key, T value)
{
return new KeyValuePair<T, T>(key, value);
}
Run Code Online (Sandbox Code Playgroud)
并得到一个错误;
找不到类型或命名空间名称"T"(您是否缺少using指令或程序集引用?)
我有一个来自数据库的字符串格式的 JSON,我只知道它是键值格式的。我事先不知道
以下是我的一些 JSON 字符串:
{"windowHandle":"current"} \\ in one String: String format{"type":"implicit","ms":100000} \\ in two String: String format {"id":"{a67fc38c-10e6-4c5e-87dc-dd45134db570}"} \\ in one String: String format {"key1": {"sub-key1": "sub-value1", "sub-key2": "sub-value2"}, "key2": {"sub-key2": "sub-value2"}} \\ in two String: Object format 所以,基本上:键值对的数量和值的类型(字符串,对象)事先是未知的。
我想将这些数据存储到我的哈希图中。我知道如果只能是 String: String 格式,我可以put在 Hashmap 中执行以下操作。
我的问题是,
HashMap <String, String>?提前致谢 !
我目前正在开发.NET Framework 4.7.2应用程序.我正在研究从给定数据结构中选择对象的LINQ查询:
List<KeyValuePair<int, Dictionary<string, object>>>
Run Code Online (Sandbox Code Playgroud)
它是动态对象的列表.我需要从列表中选择,其中的所有元素值在字典中是真实的,关键是IsDummy在这种情况下.
下图显示了调试模式下的数据结构xyz:
var result = xyz
.Select(a => a.Value)
.SelectMany(b => b)
.Where(c => c.Key == "IsDummy" && (bool)c.Value == true);
Run Code Online (Sandbox Code Playgroud)
我想选择List<KeyValuePair<int, Dictionary<string, object>>>字典中的值对象的类型为boolean并且值为true的位置.
不幸的是我当前的查询无法正常工作.
你知道如何解决这个LINQ查询吗?由于列表中的KeyValuePair和Dictionary,这有点棘手.
非常感谢你!