I have a bit of code that I have been tasked with converting to C# from VB. A snippet of mine seems like it cannot be converted from one to the other, and if so, I just don’t know how to do it and am getting a little frustrated.
Here’s some background:
OrderForm is an abstract class, inherited by Invoice (and also PurchaseOrder). The following VB snippet works correctly:
Dim Invs As List(Of OrderForm) = GetForms(theOrder.OrderID)
....
Dim inv As Invoice = Invs.Find(
Function(someInv As Invoice) thePO.SubPONumber = someInv.SubInvoiceNumber)
In C#, the best I came to converting this is:
List<OrderForm> Invs = GetForms(theOrder.OrderID);
....
Invoice inv = Invs.Find(
(Invoice someInv) => thePO.SubPONumber == someInv.SubInvoiceNumber);
However, I get the following error when I do this:
Cannot convert lambda expression to delegate type ‘System.Predicate’ because the parameter types do not match the delegate parameter types
Is there any way to fix this without restructuring my whole codebase?
I’m guessing that
OrderFormderives fromInvoice. If so, rewrite your lambda to omit the explicit type declaration inside theFind. (Can’t say with certainty for VB, but for C#, the type is not required in a lambda, it will be inferred.)Edit
Based on your comment, you’re going to have to do some casting inside the lambda and also to the result.
Or you could elect to use LINQ extension methods as opposed to
FindinList<>