Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class associations and lists

Tags:

c#

.net

mapping

I have a little C# problem. I have two classes ClassA and ClassB defined in this way :

public class ClassA
{
    private ClassB b;
    ClassB B;
    {
        get { return b; }
        set { b = value; }
    }
}

public class ClassB
{
    /* some stuff */
}

As you can see, ClassA has an instance of ClassB.

The thing is, from a list of ClassA instances, I want to access to a list of the corresponding ClassB instances. I suppose it would look like this:

IList<ClassA> listA = ...;
IList<ClassB> listB = listA.???.B;

The solution is probably obvious but I can't figure it out by myself.

Any help would be appreciated !

like image 382
Hal Avatar asked Jul 05 '10 09:07

Hal


1 Answers

You could use LINQ to do

IList<ClassB> listB = listA.Select(a => a.B).ToList();
like image 100
Jens Avatar answered Sep 28 '22 16:09

Jens