【问题标题】:dictionary array value search using lambda使用 lambda 进行字典数组值搜索
【发布时间】:2016-03-28 19:08:24
【问题描述】:

如何使用 lambda 表达式搜索作为对象的字典值。 (使用下面的类)

Dictionary<int, House[]> houseDict = new Dictionary<int, House[]>();

假设有 3 个元素,每个元素有 10 个房子。

我如何根据元素(例如)找到属于客户的房屋/房屋

这是我到目前为止所得到的,但不知道如何缩小到 houseNumber

houseDict[0].Where(s => s.GetCustomer() == theCustomer)

这不起作用 -> houseDict[0].SelectMany(s =&gt; s.GetHouseNumber()).Where(c =&gt; c.GetCustomer() == theCustomer);

public class House
{
  private static int _instances = 0;
  private int houseNumber;
  private bool sold;
  private bool reserved;
  private bool free;

  private Customer customer;

  public House(int theHouseNumber)
  {
      houseNumber = theHouseNumber;
      sold = false;
      reserved = false;
      free = true;
  }

  ~House()
  {
      _instances--;
  }

  public void SellHouse(Customer buyer)
  {
      customer = buyer;
      sold = true;
      free = false;
  }

  public void ReserveHouse(Customer reserver)
  {
      customer = reserver;
      free = false;
      sold = false;
      reserved = true;
  }

  public void ReturnHouse()
  {
      customer = null;
      free = true;
      sold = false;
      reserved = false;
  }

  public void BuyReservedHouse(Customer buyer)
  {
      sold = true;
  }

  public bool IsFree()
  {
      return free;
  }

  public bool IsReserved()
  {
      return reserved;
  }


  public int GetHouseNumber()
  {
      return houseNumber;
  }

  public void SetCustomer(Customer buyer)
  {
      customer = buyer;
  }

  public Customer GetCustomer()
  {
      return customer;
  }

}

public class Customer
{
    private static int _instances = 0;
    private String name;
    private int id = 0;

  public Customer(String customerName)
  {
    _instances++;
    name = customerName;
    id = _instances;
  }

  public String GetName()
  {
    return name;
  }

  public int GetId()
  {
    return id;
  }
}

【问题讨论】:

  • 字典的int键是什么?为什么要使用 key = 0 进行搜索?你能提供更多背景信息吗?
  • @Michael 我已经编辑了我的帖子
  • @YacoubMassad int 键是街道,例如街道 1、街道 2。使用 key[0] 只是为了提供我想要实现的示例
  • 其他一些与您的问题没有直接关系的观察结果:1) 考虑使用 get-only 属性而不是 GetZZZ() 方法,2. 如果您执行很多操作,字典可能不是最好的结构在 values 而不是 keys 中搜索。

标签: c# linq dictionary lambda linq-expressions


【解决方案1】:

如果特定客户的房屋可能分布在多个 ID 上,那么您需要搜索所有价值集合:

houseDict.SelectMany(kvp => kvp.Value)
         .Where(s => s.GetCustomer() == theCustomer)

这假定theCustomer instance 是多值集合中的同一个实例。如果您想根据 ID 进行匹配,那么您可以使用:

houseDict.SelectMany(kvp => kvp.Value)
         .Where(s => s.GetCustomer().GetId() == theCustomer.GetId())

【讨论】:

  • 谢谢您,但是单个 ID 呢?一个人可以有多个房子吗?例如他们在第一个 key/id houseDict[0] 中有 3 个房子
  • 如果它们都有相同的客户,则任一查询都可能返回多个房屋。区别在于这些房屋是否包含相同的客户实例,或具有相同 ID 的不同实例。
  • 对不起,我应该在我的最后评论中更清楚。所以在 houseDict[0] 中说 3 个 House 数组元素有 John 作为客户。那么你将如何使用 lambda 检索它。不搜索整个字典,即不搜索 houseDict[1], houseDict[2]
  • @S.Bane 上述任何查询都可以解决您的问题,除了迭代所有条目之外,您的数据结构(字典)没有其他方法。
  • @S.Bane 那就是您在问题中陈述的houseDict[0].Where(s =&gt; s.GetCustomer() == theCustomer)。我不知道您所说的“不知道如何缩小到 houseNumber”是什么意思
【解决方案2】:

听起来您想要搜索字典,而不仅仅是字典中的第一项。只要你有using System.Linq; 声明,你应该可以这样做:

houseDict.ToList().Where(s => s.Value.GetCustomer() == theCustomer);

这会将您的字典更改为列表>,您可以使用 lambda。

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2013-05-22
    • 2015-03-28
    相关资源
    最近更新 更多