C# 使用字典键/值对实现对象列表
我正在尝试使用C# 使用字典键/值对实现对象列表,c#,asp.net,list,dictionary,C#,Asp.net,List,Dictionary,我正在尝试使用字典和列表进行搜索。我知道,我可以通过List轻松做到这一点,如下所示: var con = (from c in db.Customers where c.Status == status select c).ToList(); lst.Add(aCustomer); //As database will have more result or data simply var custDictionary = new Dictio
字典
和列表
进行搜索。我知道,我可以通过List
轻松做到这一点,如下所示:
var con = (from c in db.Customers
where c.Status == status
select c).ToList();
lst.Add(aCustomer); //As database will have more result or data simply
var custDictionary = new Dictionary<string, List<Customer>>();
// the above code for the list
custDictionary.Add("keyname", lst);
但我更喜欢并尝试使用字典实现上述功能。我的概念(我们都知道)是使用键/值将提高搜索选项的性能。这看起来很简单,有点卡住了。以下是我尝试过的:
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; }
}
另一方面,我认为字典应该如下所示:
Dictionary<string, List<Customer>> custDictionary = new Dictionary<string, List<Customer>>();
Dictionary custdirectionary=newdictionary();
我的问题是,在字典中为key/vale对传递一个对象列表是一个好主意吗?我已经尝试过这样做。但是还没有得到输出
注意:这听起来像是一个新手问题,是的,是的。我试着在网上搜索,但仍在研究。我很抱歉问这样一个问题,如果有更好的方法来解决上述问题,我希望得到一些答案 更新
如果要将它们存储在列表中,可以执行以下代码。要选择项目,可以使用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);
}
已更新
如果要将它们存储在列表中,可以执行以下代码。要选择项目,可以使用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个对象来逐个创建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();
Dictionary custDictionary=新字典(StringComparer.InvariantCultureInogoreCase)//字典声明
List lst=新列表()//声明的对象列表
客户aCustomer=新客户()//已创建客户对象
/**赋值-开始**/
aCustomer.CustomerId=1001;
aCustomer.CustomerName=“John”;
aCustomer.Address=“在地球上”;
aCustomer.Status=“活动”;
custdirectionary.Add(aCustomer.Status,aCustomer)//使用键和值添加到字典中
客户b客户=新客户()//已创建客户对象
b customer.CustomerId=1002;
b customer.CustomerName=“James”;
b客户地址=“在地球上”;
b客户状态=“非活动”;
custdirectionary.Add(b customer.Status,b customer)//使用键和值添加到字典中
字符串状态=Console.ReadLine().ToUpper();
if(custdirectionary.ContainsKey(status))//在字典中找到if键
{
客户客户=客户字典[状态];
Console.WriteLine(cust.CustomerId+“”+cust.CustomerName);//输出最终结果-现在在这里找不到结果
}
Console.ReadLine();
即使将状态添加为键,代码也有两个问题
您需要创建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();
Dictionary custDictionary=新字典(StringComparer.InvariantCultureInogoreCase)//字典声明
List lst=新列表()//声明的对象列表
客户aCustomer=新客户()//已创建客户对象
/**赋值-开始**/
aCustomer.CustomerId=1001;
aCustomer.CustomerName=“John”;
aCustomer.Address=“在地球上”;
aCustomer.Status=“活动”;
custdirectionary.Add(aCustomer.Status,aCustomer)//使用键和值添加到字典中
客户b客户=新客户()//已创建客户对象
b customer.CustomerId=1002;
b customer.CustomerName=“James”;
b客户地址=“在地球上”;
b客户状态=“非活动”;
custdirectionary.Add(b customer.Status,b customer)//使用键和值添加到字典中
字符串状态=Console.ReadLine().ToUpper();
if(custdirectionary.ContainsKey(status))//在字典中找到if键
{
客户客户=客户字典[状态];
Console.WriteLine(cust.CustomerId+“”+cust.CustomerName);//输出最终结果-现在在这里找不到结果
}
Console.ReadLine();
首先,您的字典键应该是customerId而不是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();
}
static void Main(字符串[]args)
{
Dictionary custDictionary=新建字典();//已声明字典
List lst=new List();//声明的对象列表
Customer aCustomer=新客户();//已创建客户对象
/**赋值-开始**/
aCustomer.CustomerId=1001;
aCustomer.CustomerName=“John”;
aCustomer.Address=“在地球上”;
aCustomer.Status=“活动”;
if(!custDictionary.ContainsKey(aCustomer.CustomerId))
custDictionary.Add(aCustomer.CustomerId,aCustomer);
其他的
custDictionary[aCustomer.CustomerId]=aCustomer;
aCustomer.CustomerId=1002;
aCustomer.CustomerName=“詹姆斯”;
aCustomer.Address=“在地球上”;
aCustomer.Status=“非活动”;
/**赋值-结束**/
if(!custDictionary.ContainsKey(aCustomer.CustomerId))
custDictionary.Add(aCustomer.CustomerId,aCustomer);
其他的
custDictionary[aCustomer.CustomerId]=aCustomer;
字符串状态=Console.ReadLine().ToUp
string status = Console.ReadLine();
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()])
{
}