C# 字典中的两个键和一个值

C# 字典中的两个键和一个值,c#,asp.net,dictionary,data-structures,C#,Asp.net,Dictionary,Data Structures,我需要在字典中实现两个键,但有点卡住了。我不确定是否可以这样做,但我的标准是使用两个键在字典数据结构中进行搜索选项匹配,类似于下面的Linq: if(id > 0 && status != null) { var result = (from c in db.Employees where c.EmployeeId == id && c.Status == status sele

我需要在字典中实现两个键,但有点卡住了。我不确定是否可以这样做,但我的标准是使用两个键在字典数据结构中进行搜索选项匹配,类似于下面的Linq:

if(id > 0 && status != null)
{
    var result = (from c in db.Employees
                  where c.EmployeeId == id && c.Status == status
                  select c).ToList();
}
使用Dictionary,我尝试了以下方法:

public class Employee
{
   public int EmployeeId { get; set; }
   public string EmployeeName { get; set; }
   public string Address { get; set; }
   public string Status { get; set; }
}

public class TwoKeyDictionary<k1, k2, T> : Dictionary<k2, Dictionary<k2, T>>
{

}
公共类员工
{
public int EmployeeId{get;set;}
公共字符串EmployeeName{get;set;}
公共字符串地址{get;set;}
公共字符串状态{get;set;}
}
公共类TwoKeyDictionary:Dictionary
{
}
最后,尝试将Employee类与数据绑定,并使用列表:

List<Employee> employees = new List<Employee>()
{
    new Employee { EmployeeId = 1001, EmployeeName = "John", Address = "On Earth", Status = "Active"},
    new Employee { EmployeeId = 1002, EmployeeName = "Jack", Address = "On Earth", Status = "Active"},
    new Employee { EmployeeId = 1003, EmployeeName = "James", Address = "On Earth", Status = "Inactive"},
    new Employee { EmployeeId = 1004, EmployeeName = "Oswald", Address = "On Earth", Status = "Inactive"}
};

int id = 0;
string status = "" 

if (id > 0 && status != "")
{
    id = Convert.ToInt32(Console.ReadLine())
    status = Console.ReadLine().ToUpper();
}

TwoKeyDictionary<int, string, List<Employee>> dict = 
          employees.GroupBy(c => new { 
             CustomerId = c.EmployeeId, 
             c.Status })
         .ToDictionary(g => g.Key.CustomerId, g => g.Key.Status, g => g.ToList());

foreach (var item in dict[id][status])
{
    Console.WriteLine(item.CustomerId + " " + item.CustomerName); 
}
List employees=新列表()
{
新员工{EmployeeId=1001,EmployeeName=“John”,Address=“On Earth”,Status=“Active”},
新员工{EmployeeId=1002,EmployeeName=“杰克”,Address=“地球上”,Status=“活动”},
新员工{EmployeeId=1003,EmployeeName=“James”,Address=“On Earth”,Status=“Inactive”},
新员工{EmployeeId=1004,EmployeeName=“Oswald”,Address=“On Earth”,Status=“Inactive”}
};
int id=0;
字符串状态=“”
如果(id>0&&status!=“”)
{
id=Convert.ToInt32(Console.ReadLine())
status=Console.ReadLine().ToUpper();
}
TwoKeyDictionary dict=
employees.GroupBy(c=>new{
CustomerId=c.EmployeeId,
c、 状态})
.ToDictionary(g=>g.Key.CustomerId,g=>g.Key.Status,g=>g.ToList());
foreach(dict[id][status]中的var项目)
{
Console.WriteLine(item.CustomerId+“”+item.CustomerName);
}

它看起来已经完成了,但是现在,我遇到了一些异常,其中一个是:“无法将lambda表达式转换为System.Collections.Generic.IEComparer类型,因为它不是委托类型””-ToDictionary(g=>g.Key.CustomerId,g=>g.Key.Status,g=>g.ToList()。此行中的另一个错误是:dict[id][status]中的var项。是否有任何方法可以消除它,并且可能在某个地方做了错误的事情。

我想将您的字典更改为以下内容:

Dictionary<Tuple<int, string>, List<Employee>>
Dictionary<Tuple<int, string>, List<Employee>> dict = 
    employees.ToDictionary(g => 
        new Tuple<int, string>(g.Key.CustomerId, g.Key.Status), g => g);

我会把你的字典改成这样:

Dictionary<Tuple<int, string>, List<Employee>>
Dictionary<Tuple<int, string>, List<Employee>> dict = 
    employees.ToDictionary(g => 
        new Tuple<int, string>(g.Key.CustomerId, g.Key.Status), g => g);

为什么不使用
元组作为键呢

Dictionary<Tuple<K1, K2>, V> d1;
字典d1;

为什么不使用
元组作为键

Dictionary<Tuple<K1, K2>, V> d1;
字典d1;

实际上,不需要额外的类(甚至仅限于两个键)就可以做到这一点。使用使用匿名类型作为键:

var idStatusLookup = employees.ToLookup(x => new {x.EmployeeId, x.Status});
var matchingEmployees = idStatusLookup[new { EmployeeId = 1001, Status = "Active"}];

foreach (Employee emp in matchingEmployees)
{
    Console.WriteLine(emp.EmployeeId + " " + emp.EmployeeName);
}

查找类似于字典,但它允许有多个键,并且始终存在一个值,即使对于未包含的键也是如此。这怎么可能?如果键未包含,则返回
Enumerable.Empty
,并为同一键返回一系列值。

实际上,您可以在不使用其他类的情况下完成此操作它甚至仅限于两个密钥。请使用使用匿名类型作为密钥的:

var idStatusLookup = employees.ToLookup(x => new {x.EmployeeId, x.Status});
var matchingEmployees = idStatusLookup[new { EmployeeId = 1001, Status = "Active"}];

foreach (Employee emp in matchingEmployees)
{
    Console.WriteLine(emp.EmployeeId + " " + emp.EmployeeName);
}

查找类似于字典,但它允许有多个键,并且总是有一个值,即使对于不包含的键也是如此。这怎么可能?如果键不包含,则返回
可枚举的。空的
,并为同一个键返回一系列值。

列表如何?
ToDictionary
返回
字典Ary
。你不能期望它自动返回一个
TwoKeyDictionary
。列表如何?
ToDictionary
返回一个
Dictionary
。你不能期望它自动返回一个
TwoKeyDictionary
。我在这里再次出现异常-g=>g.ToList()它说-无法将lambda表达式转换为type,因为它不是委托类型它应该是
g=>g
而不是
g=>g.ToList()
,因为“g”已经是
List
了,我在这里再次遇到异常-g=>g.ToList()它说-不能将lambda表达式转换为type,因为它不是委托类型它应该是
g=>g
而不是
g=>g.ToList()
因为“g”已经是一个
列表
好了,谢谢你的想法@Tim Schmelter。那么,这是否意味着我已经尝试过避免使用或有限制。没关系,我只是想问一下,因为我不是这方面的专家。@user8512043:好吧,你看到上面的代码与你的相比有多短,而且它很有效。那么,你为什么要创建一个新的词汇呢没有哪种类型的密钥对只限于两个而不是无限?这是真的@Tim Schmelter。我只是想知道是否有可能做到。简单的事情确实更可取。再次感谢。好的,谢谢@Tim Schmelter的想法。那么这是否意味着我已经尝试过避免使用或有限制。没关系,我只是asking因为我不是这方面的专家。@user8512043:好吧,你可以看到上面的代码与你的代码相比有多短,而且它可以工作。那么为什么你要创建一个只限于两个键对而不是无限的新字典类型呢?这是真的@Tim Schmelter。我只是想知道这是否可行。简单的事情确实更可取。谢谢艾因。