我正在尝试与Dictionary<>
一起使用List<>
进行搜索。我知道,我可以使用List<>
轻松完成此操作,如下所示:
var con = (from c in db.Customers
where c.Status == status
select c).ToList();
但首选并尝试使用Dictionary<>
实现上述功能。我的概念(我们都知道)使用键/值会增加搜索选项的性能。这看起来很简单并且有点卡住了。这是我尝试过的:
static void Main(string[] args)
{
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>(); //Dictionary declared
List<Customer> lst = new List<Customer>(); //List of objects declared
Customer aCustomer = new Customer(); //Customer object created
/**Assign values - Starts**/
aCustomer.CustomerId = 1001;
aCustomer.CustomerName = "John";
aCustomer.Address = "On Earth";
aCustomer.Status = "Active";
aCustomer.CustomerId = 1002;
aCustomer.CustomerName = "James";
aCustomer.Address = "On Earth";
aCustomer.Status = "Inactive";
/**Assign values - Ends**/
custDictionary.Add(aCustomer.Status, aCustomer); //Added to the dictionary with key and value
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(status)) //If key found in the dictionary
{
Customer cust = custDictionary[status];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadKey();
}
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string Address { get; set; }
public string Status { get; set; }
}
不幸的是,上述内容并未返回任何结果。我想要的是通过传递状态键获取客户详细信息,并再次传递Customer
对象作为值。我不确定我在这里缺少什么。
还有一件事,在现实生活中,我们将数据库结果作为列表。所以在这种情况下,如果使用Dictionary<>
,我相信,数据库结果应该保持如下:
lst.Add(aCustomer); //As database will have more result or data simply
另一方面,我相信,字典应该如下所示:
Dictionary<string, List<Customer>> custDictionary = new Dictionary<string, List<Customer>>();
我的问题 - 在密钥/值对中传递字典中的对象列表是一个好主意,我尝试过这样做。但是还没有得到输出。
注意:这听起来像是一个新手问题,是的,是的。我试图在网上搜索并仍在研究它。我道歉要问这样一个问题,如果有更好的方法可以做到这一点,我会期待一些答案。
答案 0 :(得分:2)
<强>已更新强>
如果要将它们存储在列表中,可以执行以下代码。要选择项目,您可以使用Linq,这样您就不会在字典中出现重复值的问题:
var lst = new List<Customer>(); //List of objects declared
lst.AddRange(
new List<Customer>() {
new Customer()
{
CustomerId = 1001,
CustomerName = "John",
Address = "On Earth",
Status = "Active"
},
new Customer()
{
CustomerId = 1002,
CustomerName = "James",
Address = "On Earth",
Status = "Inactive"
}
}
);
var status = Console.ReadLine();
var selected = lst.Where(x => x.Status.ToUpper() == status.ToUpper()).ToList();
foreach (var item in selected)
{
Console.WriteLine(item.CustomerId + " " + item.CustomerName);
}
更新2
如果要在字典中添加上述列表,可以执行以下操作:
var custDictionary = new Dictionary<string, List<Customer>>();
// the above code for the list
custDictionary.Add("keyname", lst);
原始回答
您只保存一个客户,因为您用第二个客户覆盖了第一个客户:
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>();
List<Customer> lst = new List<Customer>();
// Add first customer
var aCustomer = new Customer()
{
CustomerId = 1001,
CustomerName = "John",
Address = "On Earth",
Status = "Active"
};
custDictionary.Add(aCustomer.Status.ToUpper(), aCustomer);
// Add second customer
var bCustomer = new Customer()
{
CustomerId = 1002,
CustomerName = "James",
Address = "On Earth",
Status = "Inactive"
};
custDictionary.Add(bCustomer.Status.ToUpper(), bCustomer);
此外,您需要将状态存储为大写,因为您正在检查状态是否以大写形式存在:
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(status)) //If key found in the dictionary
{
Customer cust = custDictionary[status];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadKey();
答案 1 :(得分:1)
即使您将状态添加为密钥,代码也会出现2个问题。
您需要创建2个对象,逐个创建2个客户。您只需添加一次客户,并分配两次值。
Console.ReadLine().ToUpper()
- 删除ToUpper()
,因为您要以大小写混合的形式添加值。如果您想这样做,请使用StringComparer.InvariantCultureIgnoreCase
初始化字典。
这对你有用。
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>(StringComparer.InvariantCultureIgnoreCase); //Dictionary declared
List<Customer> lst = new List<Customer>(); //List of objects declared
Customer aCustomer = new Customer(); //Customer object created
/**Assign values - Starts**/
aCustomer.CustomerId = 1001;
aCustomer.CustomerName = "John";
aCustomer.Address = "On Earth";
aCustomer.Status = "Active";
custDictionary.Add(aCustomer.Status, aCustomer); //Added to the dictionary with key and value
Customer bCustomer = new Customer(); //Customer object created
bCustomer.CustomerId = 1002;
bCustomer.CustomerName = "James";
bCustomer.Address = "On Earth";
bCustomer.Status = "Inactive";
custDictionary.Add(bCustomer.Status, bCustomer); //Added to the dictionary with key and value
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(status)) //If key found in the dictionary
{
Customer cust = custDictionary[status];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadLine();
答案 2 :(得分:1)
首先,你的字典键应该是customerId not status。检查字典是否包含密钥是一个很好的做法,它会抛出异常,同时添加相同的密钥。所以最好检查然后在字典中执行添加或更新。
static void Main(string[] args)
{
Dictionary<string, Customer> custDictionary = new Dictionary<string, Customer>(); //Dictionary declared
List<Customer> lst = new List<Customer>(); //List of objects declared
Customer aCustomer = new Customer(); //Customer object created
/**Assign values - Starts**/
aCustomer.CustomerId = 1001;
aCustomer.CustomerName = "John";
aCustomer.Address = "On Earth";
aCustomer.Status = "Active";
if (!custDictionary.ContainsKey(aCustomer.CustomerId))
custDictionary.Add(aCustomer.CustomerId, aCustomer);
else
custDictionary[aCustomer.CustomerId] = aCustomer;
aCustomer.CustomerId = 1002;
aCustomer.CustomerName = "James";
aCustomer.Address = "On Earth";
aCustomer.Status = "Inactive";
/**Assign values - Ends**/
if (!custDictionary.ContainsKey(aCustomer.CustomerId))
custDictionary.Add(aCustomer.CustomerId, aCustomer);
else
custDictionary[aCustomer.CustomerId] = aCustomer;
string status = Console.ReadLine().ToUpper();
if (custDictionary.ContainsKey(aCustomer.CustomerId)) //If key found in the dictionary
{
Customer cust = custDictionary[aCustomer.CustomerId];
Console.WriteLine(cust.CustomerId + " " + cust.CustomerName); //Outputs the final result - Right now no result found here
}
Console.ReadKey();
}
答案 3 :(得分:1)
您没有获得任何输出,因为您将输入转换为大写,而您在pascalcase中插入了键,并且在C#集合键的情况下区分大小写。这样你的输入就不会与集合中的任何键匹配
将您的行号更改为:29到此代码
string status = Console.ReadLine();
并从您的控制台插入“非活动”,此密钥存在于您的集合中 所以你会想要结果..
答案 4 :(得分:1)
如果已经拥有该列表,并且想要创建一个Dictionary<string, List<Customer>>
,您可以这样做:
Dictionary<string, List<Customer>> dict =
list.GroupBy(c=>c.Status.ToUpper()).ToDictionary(g => g.Key, g=> g.ToList());
迭代它:
foreach (var customer in dict[status.ToUpper()])
{
}
<强>但是,强>
我没有看到这样做的价值。如果你需要让具有特定状态的所有客户保持你所拥有的 - 一个简单的linq查询。