我是新工作的c#,我有使用c ++和java的经验。我试图乱用字典,但我真的无法使用它。我有两个数组,数据类型必须是对象,我把它们添加到两个不同的词典后我试图找到一个键,但我不能让它进入if语句。字典的两个声明中的哪一个是正确的dictionary1还是字典2?另外,我如何通过键或键找到正确字典中的值或两者的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice_With_Dictionaries
{
class Program
{
static void Main(string[] args)
{
object[] array1 = new object[5];
array1[0] = "1111";
array1[1] = "2222";
array1[2] = "3333";
array1[3] = "4444";
array1[4] = "5555";
object[] speed = new object[5];
speed[0] = 1;
speed[1] = 2;
speed[2] = 3;
speed[3] = 4;
speed[4] = 5;
object[] keys = new object[1];
keys[0] = (object[])array1;
object[] speedTable = new object[1];
speedTable[0] = (object[])speed;
Dictionary<object, object> dictionary1 = new Dictionary<object, object>();
Dictionary<object[], object[]> dictionary2 = new Dictionary<object[], object[]>();
dictionary1.Add(keys, speedTable);
dictionary2.Add(keys, speedTable);
if (dictionary1.ContainsKey((object)"1111"))
{
var method = 1;
}
if (dictionary2.ContainsKey(array1))
{
var method = 2;
}
}
}
}
答案 0 :(得分:1)
def ordergen(portfolio):
for i in portfolio:
{
"accountID" : "02e57c7d-d071-4c63-b491-1194a9939ea5.1452548617456",
"accountNo" : "DWTE000005",
"userID" : "02e57c7d-d071-4c63-b491-1194a9939ea5",
"accountType" : 2,
"ordType" : "1",
"side" : "B",
"instrumentID" :fundID[i],
"orderQty" : ,
"comment":""
}
永远不会返回true,因为dictionary1.ContainsKey((object)"1111")
每次都会被装入一个新的唯一对象。
您可以一次填充一个项目的字典:
"1111"
您也可以使用LINQ和ToDictionary(IEnumerable<TSource, Func<TSource, TKey>, Func<TSource, TElement>)方法填充没有显式循环的字典,该方法根据指定的键选择器和元素选择器函数从IEnumerable创建一个Dictionary。
Dictionary<object, object> dictionary1 = new Dictionary<object, object>();
for (int i = 0; i < array1.Length; i++)
{
dictionary1.Add(array1[i], speed[i]);
}
object key1 = array1[0];
if (dictionary1.ContainsKey(key1))
{
var method = 1;
}
答案 1 :(得分:0)
您的代码面临的挑战是您以数组或其他形式传递键值,它们使用的是列表。通常我们以一对一的关系初始化一个Dictionary:
Dictionary<object, object> dict = new Dictionary<object, object>();
有时在一对多关系中:
Dictionary<object, object[]> dict = new Dictionary<object, object[]>();
在您的情况下,您可以在多对多关系中初始化它:
Dictionary<object[], object[]> dictionary2 = new Dictionary<object[], object[]>();
虽然在你的第一个例子中,特别是dictionary1,你仍然在你的TKey值上传递一个数组(见你的代码):
Dictionary<object, object> dictionary1 = new Dictionary<object, object>();
dictionary1.Add(keys, speedTable); //the value of keys consists of an object of an array: keys[0] = (object[])array1;
因此,您最好的方法是使用单个对象的TKey和对象的TValue或对象数组或对象列表来实现Dictionary。
如果你仍然想要做一个对象数组,你需要实现一个自定义IEqualityComparer,因为你不能在if语句中做你想做的事情。
这是一个示例通用实现,您需要在构造函数Dictionary中提供IEqualityComparer:
public class DictionaryComparer<T> : IEqualityComparer<List<T>>
{
public bool Equals(List<T> x, List<T> y)
{
//TODO: Your implementation for your contains or equals condition
}
public int GetHashCode(List<T> obj)
{
//TODO: Implementation of your GetHashCode
}
}
然后实施它:
if (new DictionaryComparer<object>().Equals(lstCompare, lstCompareTo))
{
//TODO: Your condition here..
}