Lambda Expression for FirstOrDefault with two parameters
You can call your Lambda function this way.
public GetRow()
{
T currentRow = CompareRow;
var firstRow = Rows.First(row => Lambda(row, CompareRow)); // get first row that matches Lambda
SelectedRow = firstRow;
}
Here is another example using string
parameters:
List<string> names = new() { "Alice", "Bob", "Charlie" };
string nameToMatch = "Alice";
Func<string, string, bool> Lambda = (left, right) => left.GetHashCode() == right.GetHashCode();
var alice = names.First(name => Lambda(name, nameToMatch));
Console.WriteLine($"Hi {alice}");
You may have some issues setting Lambda. The type looks wrong Func<Entity, Entity, bool>
is not Func<T, T, bool>
as there are no constraints on what type T
is.
You may want to consider adding a constraint on T
, maybe something like this:
public class Model<T>
where T : Entity
{
public Func<Entity, Entity, bool> Lambda { get; set; }
}