小编Roh*_*ela的帖子

首先在表达式树中使用 ToString 方法

我是表达式树的新手,我需要将下面的 lambda 转换为表达式树

Data.Where(s => s.Property.ToString().StartsWith("My Search Data"));
Run Code Online (Sandbox Code Playgroud)

但是我已经完成了

Data.Where(s => s.Property.StartsWith("My Search Data");
Run Code Online (Sandbox Code Playgroud)

现在我需要在使用 StartsWith 之前使用 ToString 方法。

下面是我的示例代码。

ParameterExpression e = Expression.Parameter(typeof(T), "e");
PropertyInfo propertyInfo = typeof(T).GetProperty(field);
MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
ConstantExpression c = Expression.Constant(data, typeof(string));
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
Expression call = Expression.Call(m, mi, c);
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(call, e);
query = query.Where(lambda);
Run Code Online (Sandbox Code Playgroud)

c# expression-trees

2
推荐指数
1
解决办法
2332
查看次数

标签 统计

c# ×1

expression-trees ×1