Search code examples
c#linqfscheck

c# FsCheck cannot convert lambda expression


I'm trying to get a C# FsCheck generator to generate a series of commands which are initialized with random strings. I came up with the following solution:

public Gen<Command<A,B>> Next(B value)
{
  var gen1 = Arb.Default.String().Generator;
  var gen2 = Gen.two(gen1);
  var gen3 = gen2.select((Command<A,B>)(s => new DerivedCommand(s.Item1,s.Item2))) 
  //DerivedCommand extends Command<A,B>

  return Gen.OneOf(gen3);
}

However, VS cannot build this code:

Cannot convert lambda expression to type Command<A,B> because it is not a delegate type

I have searched solutions for this error message, but nothing I found helped. I am using System.Linq and System.Data.Entity. Any suggestions for resolving this issue are much appreciated.


Solution

  • You're trying to cast (s => new DerivedCommand(s.Item1,s.Item2)), which is a lambda expression, to (Command<A,B>), which (I assume) is a class.

    You probably need something like:

    var gen3 = gen2.select(s => (Command<A,B>)(new DerivedCommand(s.Item1,s.Item2)));