The easiest way would be to just declare it as an Expression<Func<...>>
public static class Program {
public static void Main() {
Expression<Func<DummyClass, Boolean>> predicate = WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800");
}
}
But if you want to construct it using different Expressions...
public static class Program {
public static void Main() {
var param = Expression.Parameter(typeof(DummyClass), "WageConstIn");
var constValue = Expression.Constant("2800");
// WageConstIn => WageConstIn.Serialno.ToString().StartsWith(...)
var first = Expression.Lambda(
parameters: param,
body: Expression.Call(
instance: Expression.Call(
instance: Expression.Property(param, "Serialno"),
methodName: "ToString",
typeArguments: null,
arguments: null
),
methodName: "StartsWith",
typeArguments: null,
arguments: new[] { constValue }
)
);
// WageConstIn => Convert.ToString(WageConstIn.Serialno).StartsWith(...)
var second = Expression.Lambda(
parameters: param,
body: Expression.Call(
instance: Expression.Call(
type: typeof(Convert),
methodName: "ToString",
typeArguments: null,
arguments: new[] { Expression.Property(param, "Serialno") }
),
methodName: "StartsWith",
typeArguments: null,
arguments: new[] { constValue }
)
);
}
}
Most people [that I've talked to] who enter the domain of expression trees are usually satisfied with the System.Linq.Dynamic functionality. (Which can be abused into a lot of different ways.) This code snippet of pure awesomeness is a part of the Visual Studio sample code, probably hiding somewhere on your harddrive already.