如何从Dictionary>中查找值

本文关键字:Struct 查找 IEnumerable Dictionary int | 更新日期: 2023-09-27 18:19:14

我有一个字典,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; }
 }

如何从Dictionary<int, IEnumerable<Struct>>中查找值

IEnumerable<SalaryFitmentInfoMonth> salaries = salaryFitmentDictionary[1];
SalaryFitmentInfoMonth salary = salaries.FirstOrDefault(s => s.EDId == 45);

您应该处理salaryFitmentDictionary不包含此ID的情况。所以你可以用TryGetValue代替。如果没有这个EDId, FirstOrDefault返回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

您可以在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();