- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有以下正常表达式:
var result = SomeList.Where(item => item.Status.Description.Contains("READY"));
对于这些对象:
public class Movie
{
public MovieStatus Status {get; set;}
}
public class MovieStatus
{
public string Description {get; set;}
}
这行不通:
ParameterExpression pe = Expression.Parameter(typeof(T), "item.Status");
MemberExpression propExp = Expression.Property(pe, "Description");//boem! Description is not a property of T.
通过 T
属性的一些递归,我可以获得正确的 MemberExpression
并且在调试时它看起来没问题,最后我有这个 lambda 表达式:
{item => item.Status.Description.Contains("READY")}
并且,当将这些表达式应用于 IQueryable
列表时,这将是结果:
{System.Collections.Generic.List`1[Movie].Where(item => item.Status.Description.Contains("READY"))}
看起来没问题,但是在编译/执行列表中的表达式时,出现以下错误:
Additional information: variable 'item.Status' of type 'MovieStatus' referenced from scope '', but it is not defined
我需要如何“链接”ParameterExpression
以获得上述 lambda 表达式?
真正的代码没有这些固定变量,它是一个通用的实现,可以被任何具有任何子属性的对象使用。输入是正常格式 XX.YY 的属性名称和一个比较值。发布所有代码有点庞大,但下面是其中的一个子集,消除了所有递归以专注于问题。递归的一些结果已在此处进行了硬编码。此外,它并不总是包含
。
public static void Test<T>(IQueryable<T> source)
{
string propertyName = "Status.Description";
string value = "READY";
ParameterExpression pe = Expression.Parameter(typeof(T), "item");
Type type = typeof(T).GetProperty("Status").PropertyType;//property name is some recursion result
ParameterExpression peSub = Expression.Parameter(type, "item.Status");
MemberExpression propExp = Expression.Property(peSub, "Description");
Expression whereValue = GetValueExpression(value, type);
//do the contains rule expression
Type subType = type.GetProperty("Description").PropertyType;//property name is also recursion result
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { subType });
Expression ruleExpression = Expression.Call(propExp, containsMethod, whereValue);
//create source.Where([expressions])
Type[] elementTypes = new Type[] { source.ElementType };
Expression<Func<T, bool>> labdaExpression = Expression.Lambda<Func<T, bool>>(ruleExpression, new ParameterExpression[] { pe });
//method call expression
Expression whereCallExpression = Expression.Call(typeof(Queryable), "Where",
elementTypes, source.Expression, labdaExpression);
source = source.Provider.CreateQuery<T>(whereCallExpression);
source.ToList();//boom, error: Additional information: variable 'item.Status' of type 'MovieStatus' referenced from scope '', but it is not defined
}
最佳答案
where
子句中只有一个参数。让我们先创建它:
public static IQueryable<T> Where<T>(this IQueryable<T> query, string selector, string comparer, string value)
{
var target = Expression.Parameter(typeof(T));
return query.Provider.CreateQuery<T>(CreateWhereClause(target, query.Expression, selector, comparer, value));
}
对于参数,我们需要创建那个子句,它实际上是一个调用表达式,它“引用”实际的 lambda:
static Expression CreateWhereClause(ParameterExpression target, Expression expression, string selector, string comparer, string value)
{
var predicate = Expression.Lambda(CreateComparison(target, selector, comparer, value), target);
return Expression.Call(typeof(Queryable), nameof(Queryable.Where), new[] { target.Type },
expression, Expression.Quote(predicate));
}
lambda 表达式应该包含实际比较,其中包含左侧的成员访问权限和右侧的实际值:
static Expression CreateComparison(ParameterExpression target, string selector, string comparer, string value)
{
var memberAccess = CreateMemberAccess(target, selector);
var actualValue = Expression.Constant(value, typeof(string));
return Expression.Call(memberAccess, comparer, null, actualValue);
}
对于成员访问,我们可以链接这些属性表达式:
static Expression CreateMemberAccess(Expression target, string selector)
{
return selector.Split('.').Aggregate(target, (t, n) => Expression.PropertyOrField(t, n));
}
最后,您应该能够:
query.Where("Status.Description", "Contains", "READY");
顺便说一句,我刚刚简化了 this code希望能给出相应的答案。
关于c# - 使用 Expression.Parameter 中的子属性构建 linq 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090303/
我的 Web 应用程序在后端使用 Node.js 和 Express。当违反内容安全策略 (CSP) 时,报告 URI 报告空对象。我的后台代码如下: app.use(bodyParser.urlen
在服务器端提供静态服务的方式在 Express 中似乎非常简单: To serve static files such as images, CSS files, and JavaScript fil
var express = require('express'); var app = express(); 这就是我们创建快速应用程序的方式。但是这个'express()'是什么?它是方法还是构造函
我在尝试安装时收到以下错误 express : npm ERR! code ERR_OSSL_PEM_NO_START_LINE npm ERR! errno ERR_OSSL_PEM_NO_STAR
如 express 所述routing guide和 this answer ,我们可以创建“迷你应用程序”并从主应用程序使用它。但是我看到一段代码,它在模块中使用 app 而不是 router ap
我正在写一个 NestJS应用。现在我想安装 Express中间件 express-openapi-validator . 但是,我无法让它工作。有一个 description for how to
我看过很多类似的帖子,似乎我声明的 var1 似乎需要在其他地方传递,但我似乎无法弄清楚。 public Expression> CreateEqualNameExpression(string ma
Express(或 Connect 的)bodyParser 中间件被标记为已弃用,建议用户改用: app.use(connect.urlencoded()) app.use(connect.json
我只是想知道这种看似尴尬的配置的原因是什么(来自 Getting Started w/ Apollo Server ), const server = new ApolloServer({ //
我正在尝试在表单组中写入表单控件特定的验证错误消息。我在网上找到了几个教程和示例 ( such as this one ),概述了一个看似简单的 *ngIf div,如果在控件上检测到错误,则显示错误
我有一个简单的 Express 应用程序,托管在 AWS 上,使用无服务器框架。 我正在使用 serverless-http 包装 express 应用程序以部署到 AWS lambda 函数,并使用
我最近在 mozilla 教程的帮助下安装了 node 和 express。我正在安装应用程序生成器的下一步,但是当我运行时 npm install express-generator -g 在我的终
我遇到过两种不同的方式来定义 express、use() 中间件,我想知道它们之间是否有任何区别,或者它是否只是语法糖? 一个 const app = express(); app.use(cors(
我试图让我的 Jade 模板编写一个相对于当前 URL 的超链接 ( )。 例如,我的 View 是从 http://localhost/cats 调用的它看起来像这样: extends layou
检查 Express 文档我在下面看到了这种解决方案: app.all('/*', function(req, res) { console.log('Intercepting request
我似乎无法弄清楚如何包含多个模型。 我有三个模型。Tabs, Servers, and PointsTabs hasMany ServerServers belongsTo Tabs and hasM
我已使用Web PI安装IIS Express。在托盘中,没有IIS Express图标。如何在不使用命令行的情况下启动IIS Express?我希望IIS永久运行,因此没有命令行。 最佳答案 参见R
我不想在我的网站上使用 Jade 或 EJS。如何在不默认使用 Jade 模板的情况下创建快速站点?谢谢 最佳答案 如果您想要的是直接为静态 html 文件提供缓存资源的可能性,同时仍然能够点击“/”
Express是否支持HTTP动词“PATCH”,例如: app.patch("/api/resource", function(req, res){ ... }); 我检查了文档,对我来说似乎还不清
我正在快速服务器中运行 vue SPA。问题是当使用历史模式并刷新页面时,我得到一个 404 not found 异常。我尝试使用 connect-history-api-fallback 但不起作用
我是一名优秀的程序员,十分优秀!