Search code examples
c#linq

How can I use LINQ / C# to pivot a list of ObjectA with an ObjectB so that I return a list of ObjectB, each with its own list of ObjectA


Sorry, I asked a similar question to this a moment ago but I realised I got things the wrong way around...

I have a list of object A and within this list is a property that is of type object B. Object B in turn contains a list of Object A.

So here are my two classes:

public class ObjectA
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Other redacted properties that also need to be returned]
    public int ObjectBId { get; set; }
    public ObjectB ObjectB { get; set; }
}

public class ObjectB
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Other redacted properties that also need to be returned]
    public List<ObjectA> ObjectA { get; set; }
}

If I have a list of ObjectA, each with an ObjectBId/ObjectB, where some of those ObjectBs might be shared between ObjectAs, how can I pivot this list so that I can return a list of distinct ObjectB, each with their own list of ObjectA.

For example, if I have a list of ObjectAs like so:

Id ObjectBId
1 1
2 2
3 2

How can I return a list of 2 ObjectBs, where ObjectB 1 has a list with one ObjectA (Id: 1) and ObjectB 2 has a list with two ObjectAs (Ids: 2 + 3). I need to return all the properties associated with ObjectA, not just the ID.

My attempt to include reproducible code for the set up of ObjectA list:

List<ObjectA> listObjectA = new List<ObjectA>
{
    new ObjectA { Id = 1, Name = "ObjectA1", ObjectBId = 1, ObjectB = new ObjectB { Id = 1, Name = "ObjectB1" } },
    new ObjectA { Id = 2, Name = "ObjectA2",  ObjectBId = 2, ObjectB = new ObjectB { Id = 2, Name = "ObjectB2" } },
    new ObjectA { Id = 3, Name = "ObjectA3",  ObjectBId = 2, ObjectB = new ObjectB { Id = 2, Name = "ObjectB2" } },
};

Solution

  • base on your verbal requirements/description. i created a prototype. for each B object, there is a list of A-objects (the B3 object would be empty in example, because there is no relation )

    void Main()
    {
        var lstA = new List<ObjectA>(){
            new ObjectA(){Id = 1, Name = "A 1", ObjectBId = 1},
            new ObjectA(){Id = 2, Name = "A 2", ObjectBId = 2},
            new ObjectA(){Id = 3, Name = "A 3", ObjectBId = 2}
        };
        var lstB = new List<ObjectB>(){
            new ObjectB(){Id = 1, Name = "B 1"},
            new ObjectB(){Id = 2, Name = "B 2"},
            new ObjectB(){Id = 3, Name = "B 3"}
        };
    
        lstB.Select(b => new {
            ID = b.Id,
            Name = b.Name,
            ListOfObjectA = lstA.Where(w=> w.ObjectBId == b.Id).ToList()})
        .Dump();
    }
    
    public class ObjectA
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public int ObjectBId { get; set; }
        public ObjectB ObjectB { get; set; }
    }
    
    public class ObjectB
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public List<ObjectA> ObjectA { get; set; }
    }
    

    enter image description here