0

Suppose I have the following classes:

public class X
{
  public string a;
  public string b;
}

public class Y
{
  public string c;
}

and I have a Map:

var myMap = Map<X, Y>();

How do I return a list of a (IEnumerable<string>) from this?

I am trying to write:

myMap.Keys.Select(_ => _.a);

But this won't work. What am I missing?

joe_maya
  • 185
  • 1
  • 7

1 Answers1

0

The code you've written actually works fine as long as you are using System.Linq. However, if you want to create a non-empty map, your key class (X) has to derive from Record<T> as well, like this:

using System;
using System.Linq;

using LanguageExt;
using static LanguageExt.Prelude;

public class X : Record<X>
{
    public string a;
    public string b;
    public static X Create(string a, string b)
        => new X() { a = a, b = b };
}

public class Y
{
    public string c;
    public static Y Create(string c)
        => new Y() { c = c };
}

class Program
{
    static void Main(string[] args)
    {
        var myMap =
            Map(
                (X.Create("a1", "b1"), Y.Create("c1")),
                (X.Create("a2", "b2"), Y.Create("c2")));
        foreach (var str in myMap.Keys.Select(_ => _.a))
        {
            Console.WriteLine(str);
        }
    }
}

Output is:

a1
a2
Brian Berns
  • 15,499
  • 2
  • 30
  • 40