3

I am developing an application using asp.net core webapi and azure cosmosdb. I need to find an item from the list of objects. I am not experienced with linq. In the below json, i need to find a sector id who got a learnerId": "123". I am expecting output like:

"id": "7d65fa4c-86ed-4e07-9e9a-a1583df5715c"

Sector:

[
  {
    "name": "Emp1",
    "edsNumber": "",
    "sectors": [
      {
        "id": "7d65fa4c-86ed-4e07-9e9a-a1583df5715c",
        "name": "Technology1",
        "learners": [
          {
            "learnerId": "123",
            "learnerName": "Learner1",
            "dateAdded": 1612872051
          },
          {
            "learnerId": "456",
            "learnerName": "Learner2",
            "dateAdded": 1612877369
          },
          {
            "learnerId": "789",
            "learnerName": "Learner3",
            "dateAdded": 1612877372
          },
          {
            "learnerId": "12314",
            "learnerName": "Learner4",
            "dateAdded": 1613065233
          },
          {
            "learnerId": "2346",
            "learnerName": "Learner5",
            "dateAdded": 1613577350
          }
        ],
        "course": {
          "name": "Angularv10",
          "isCompleted": true
        },
        "contact": {
          "name": "Voytek",
          "jobTitle": "Designer1",
          "email": "[email protected]",
          "isCompleted": true
        }
      }
    ]
  },
  {
    "name": "Emp2",
    "edsNumber": "",
    "sectors": [
      {
        "id": "7d65fa4c-86ed-4e07-9e9a-a1583df5235c",
        "name": "Technology1",
        "learners": [
          {
            "learnerId": "834457",
            "learnerName": "Learner6",
            "dateAdded": 1612872051
          },
          {
            "learnerId": "11122",
            "learnerName": "Learner7",
            "dateAdded": 1612877369
          }
        ],
        "course": {
          "name": "Angularv10",
          "isCompleted": true
        },
        "contact": {
          "name": "Voytek",
          "jobTitle": "Designer1",
          "email": "[email protected]",
          "isCompleted": true
        }
      }
    ]
  }
]

I tried the following:

        var sectorId = (from e in employers
                      from s in e.Sectors
                      from l in s.Learners
                      where l.LearnerId == learnerEntity.Id
                      select s.Id).FirstOrDefault();

Is there a better way to achieve? And how to get both the SectorId and the employer the sector belongs to?

2 Answers 2

7

LINQ query that looks for a single item that satisfies a condition on a child object would have two components:

  • A call to SingleOrDefault (or Single if the item you look for must be there), and
  • A call to Any nested inside the first condition to see that the child item is there.

Since your sectors are embedded inside employers, you could flatten the list with SelectMany.

Here is how it would look:

var sectorId = employers
    .SelectMany(e => e.Sectors) // Flatten sectors
    .SingleOrDefault(
        s => s.Learners.Any(sl => sl.Id == targetId)
    ).Id;
Sign up to request clarification or add additional context in comments.

3 Comments

Due to the inexperience of the poster I would like to add on that SingleOrDefault will throw an exception of more than 1 element is found.
@Sergey Thanks for your reply. Along with the sectorId i need the employer name to which the sectorid belongs? Can you please help me get this as well? Thanks
@MukilDeepthi If you'd like to get the employer as well, use the trick with Any(...) two times - var employer = employers.SingleOrDefault(e => e.Sectors.Any(s => s.Learners.Any(...)). Once you have the employer, run the second query on the employer.Sectors` to get the sector.
0

This is not very clear in the question, but "sectors" is obviously an array. If an employee has more than one sector and the searched LearnerId is expected to appear in two or more sectors, then, returning an enumeration of sectorId will be necessary :

(Slithly different version based on Sergey Kalinichenko answer):

IEnumerable<Guid> sectorIds = employers
.SelectMany(e => e.Sectors) // Flatten sectors
.Where(s => s.Learners.Any(sl => sl.Id == targetId))
.Select(s => s.Id);

Comments

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.