0

I have the following JSON that gets put into a list of objects. Each object has one property and a list of codes. I’m struggling to figure out a way to query the list to find a specific code value for a specific company. I’m ultimately looking for the “AbsenceType” value based on the Company/PayCode. This is what I’ve tried to look at but it’s not working. Looking for any suggestions.

companyAbsenceType = AbsenceCodesList.First(c => 
    (c.Company == companyCode) && (c.Codes.Find(x => x.PayCode == ppc.PayCode));

companyAbsenceType = AbsenceCodesList.Select(c =>
   c.Company == companyCode && c.Codes.Find(x => x.PayCode == ppc.PayCode)).FirstOrDefault();

JSON:

[
  {
    "Company": "Company1",
    "Codes": [
      {
        "PayCode": "SCK",
        "AbsenceType": "Illness"
      },
      {
        "PayCode": "VAC",
        "AbsenceType": "Vacation"
      },
      {
        "PayCode": "BRV",
        "AbsenceType": "Bereavement"
      },
      {
        "PayCode": "JUR",
        "AbsenceType": "Jury Duty"
      },
      {
        "PayCode": "PER",
        "AbsenceType": "Personal"
      }
    ]
  },
  {
    "Company": " Company2",
    "Codes": [
      {
        "PayCode": "SCK",
        "AbsenceType": "Sick"
      },
      {
        "PayCode": "VAC",
        "AbsenceType": "Vacation"
      },
      {
        "PayCode": "BRV",
        "AbsenceType": "Bereavement"
      },
      {
        "PayCode": "JUR",
        "AbsenceType": "Jury Duty"
      },
      {
        "PayCode": "PER",
        "AbsenceType": "Personal"
      },
      {
        "PayCode": "PRNU",
        "AbsenceType": "Personal"
      }
    ]
  }
]

public class AbsenceCodes
    {
        public string Company { get; set; }
        public List<AbsenceCode> Codes { get; set; }
    }
public class AbsenceCode
    {
        public string PayCode { get; set; }
        public string AbsenceType { get; set; }
    }

UPDATE Thanks to Moho and Eric Magers pointing me to a query. The query from Moho worked.

var absenceType = AbsenceCodesList.FirstOrDefault(c => c.Company == companyCode && c.Codes.Any(x => x.PayCode == ppc.PayCode)) ?.Codes.First(c => c.PayCode == ppc.PayCode) ?.AbsenceType;

2
  • 1
    Assuming you're trying to select only the AbsenceType and do not need any of the company info, you'd first want to select the proper company, and then get the absence type: AbsenceCodesList.FirstOrDefault(c => c.Company == companyCode)?.FirstOrDefault(ac => ac.PayCode == ppc.PayCode)?.AbsenceType; It looks like both of your expressions would return the company, you would still need to do the second select the matching AbsenceTypes after that Commented Jan 21, 2022 at 17:02
  • What is the expected output of your query? Commented Jan 21, 2022 at 17:12

1 Answer 1

1

You were close, use .Any instead of .Find for the Codes when filtering:

var absenceType = AbsenceCodesList
    // first find a valid top level item
    .FirstOrDefault(c => 
        // is specific company
        c.Company == companyCode
        // has the target paycode
        && c.Codes.Any(x => x.PayCode == ppc.PayCode))

    // then select the desired value
    ?.Codes.First(c => c.PayCode == ppc.PayCode)
    .AbsenceType;
Sign up to request clarification or add additional context in comments.

3 Comments

Still working on getting it but what does the "?" in the selecting value for? Not that I'm an advanced user but I've never seen the single "?" in the query before.
The item you’re looking for may not exist, so I used .FirstOrDefault for the initial query which will return the default value should a matching value not exist. In this case, the default value will be NULL. ?. is the null propagation operator. It will check for null before invoking the next call and return null if it is null.
Thanks. I've used ?? for checking NULL values in C# but didn't know about ? for NULL values in LINQ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.