【问题标题】:How to find value from a Dictionary<int, IEnumerable<Struct>>如何从 Dictionary<int, IEnumerable<Struct>> 中查找值
【发布时间】:2014-08-22 11:00:43
【问题描述】:

我有一本字典 salaryFitmentDictionary,我想根据以下示例查询(linq 或 lambda):其中 employeedId = 1EarningDeductionId = 145 并获取余额的值 EDBalance

我将如何实现这一目标?

var balance = salaryFitmentDictionary.Where...

Dictionary<int, IEnumerable<SalaryFitmentInfoMonth>> salaryFitmentDictionary = new Dictionary<int, IEnumerable<SalaryFitmentInfoMonth>>();

employeeIdList.ToList().ForEach(employeedId =>
{
    var perEmployeeFitments = from pf in _db.PayFitments.AsEnumerable()
                              join ed in _db.EarningDeductions.AsEnumerable()
                              on pf.EarningDeductionId equals ed.EarningDeductionId
                              where pf.EmployeeId == employeedId
                              select new SalaryFitmentInfoMonth
                              {
                                   EDId = pf.EarningDeductionId,
                                   EDAmount = pf.Amount,
                                   EDBalance = pf.Balance.GetValueOrDefault(),
                                   EDType = ed.EDType,
                                   IsTaxable = ed.IsTaxable,
                                   IsBenefit = ed.IsBenefit,
                                   IsLoan = ed.IsLoan,
                                   IsAdvance = ed.IsAdvance,
                                   Limit = ed.TaxIfMoreThan.GetValueOrDefault()
                              };

    salaryFitmentDictionary.Add(employeedId, perEmployeeFitments);
});

public struct SalaryFitmentInfoMonth
{
    public int EDId { get; set; }
    public decimal EDAmount { get; set; }
    public decimal? EDBalance { get; set; }
    public EarnDeduct EDType { get; set; }
    public bool IsTaxable { get; set; }
    public bool IsBenefit { get; set; }
    public bool IsLoan { get; set; }
    public bool IsAdvance { get; set; }
    public decimal? Limit { get; set; }
 }

【问题讨论】:

  • ToList().ForEachಠ_ಠ

标签: c# linq dictionary lambda


【解决方案1】:
IEnumerable<SalaryFitmentInfoMonth> salaries = salaryFitmentDictionary[1];
SalaryFitmentInfoMonth salary = salaries.FirstOrDefault(s => s.EDId == 45);

您应该处理salaryFitmentDictionary 不包含具有此ID 的情况。所以你可以改用TryGetValue。如果没有工资有这个EDIdFirstOrDefault返回null。

所以这里是更安全的版本:

IEnumerable<SalaryFitmentInfoMonth> salaries;
if(salaryFitmentDictionary.TryGetValue(1, out salaries))
{
    SalaryFitmentInfoMonth salary = salaries.FirstOrDefault(s => s.EDId == 45);
    if(salary != null) 
    {
        // do something ...
    }
}

如果您预计会有多个匹配项,您可以使用 Enumerable.Where 而不是 FirstOrDefault

【讨论】:

    【解决方案2】:

    您可以在 LINQ 方法语法中使用 SelectMany 方法:

    Int32 id = 1;
    Int32 edId = 147;
    
    var result = salaryFitmentDictionary.
        Where((pair) => pair.Key == id ).
        SelectMany((pair) =>
            pair.Value.Where((perEmployeeFitment) => perEmployeeFitment.EDId == edId)).
        Select(perEmployeeFitment => perEmployeeFitment.EDBalance).
        Single();
    

    或者在查询语法中:

    Int32 id = 1;
    Int32 edId = 147;
    
    var result = (from pair in salaryFitmentDictionary
        from perEmployeeFitment in pair.Value
        where pair.Key == id
        where perEmployeeFitment.EDId == edId
        select perEmployeeFitment.EDBalance).Single();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多