- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想创建一个通用服务,该服务仅通过其装饰属性(通过自定义属性)在特定实体中进行搜索我尝试使用表达式来做到这一点,但我想我遗漏了一些东西。这里有一个示例代码:
服务
public class MyService<TEntity>
where TEntity: class
{
private readonly MyContext context;
public MyService()
{
this.context = new MyContext();
}
private Func<TEntity, string, bool> searchFunc = (e, search) =>
{
var type = typeof(TEntity);
var props = type.GetProperties().Where(p => p.GetCustomAttributes(typeof(SearchPropertyAttribute), true).Any());
Expression expression = Expression.Constant(false);
foreach (var prop in props)
{
expression = Expression.MakeBinary(ExpressionType.Or,
expression,
Expression.MakeBinary(ExpressionType.Equal,
Expression.Property(Expression.Constant(e), prop.Name),
Expression.Constant(search)));
}
var delegateFunc = Expression.Lambda<Func<TEntity, string, bool>>(expression).Compile();
return delegateFunc(e, search);
};
/// <summary>
/// searches in specified entity set the given string using only decorated properties.
/// </summary>
public List<TEntity> Find(string search)
{
var list = this.context.Set<TEntity>().Where(e => searchFunc(e as TEntity, search));
return list.ToList() as List<TEntity>;
}
}
实体
public class MyEntity
{
[SearchProperty]
public string FirstName { get; set; }
[SearchProperty]
public string LastName { get; set; }
public string Description { get; set; }
}
我只想将搜索限制在由 SearchProperty 属性修饰的属性上谢谢
最佳答案
我在最近的项目中实现了该功能,为此我在我的基本 Controller 中创建了以下方法:
获取实体的可过滤属性:
private List<PropertyInfo> GetFilterProps()
{
var t = typeof(TEntity);
var props = t.GetProperties();
var filterProps = new List<PropertyInfo>();
foreach (var prop in props)
{
var attr = (SearchProperty[])prop.GetCustomAttributes(typeof(SearchProperty), false);
if (attr.Length > 0)
{
filterProps.Add(prop);
}
}
return filterProps;
}
使用表达式树生成动态查询:
private IQueryable<TEntity> Filter(IQueryable<TEntity> query, string value)
{
if (string.IsNullOrEmpty(value) || this.filterProps.Count == 0) return query;
ConstantExpression constant = Expression.Constant(value.ToLower());
ParameterExpression parameter = Expression.Parameter(typeof(TEntity), "e");
MemberExpression[] members = new MemberExpression[filterProps.Count()];
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
MethodInfo toLowerMethod = typeof(string).GetMethod("ToLower", System.Type.EmptyTypes);
for (int i = 0; i < filterProps.Count(); i++)
{
members[i] = Expression.Property(parameter, filterProps[i]);
}
Expression predicate = null;
foreach (var member in members)
{
//e => e.Member != null
BinaryExpression notNullExp = Expression.NotEqual(member, Expression.Constant(null));
//e => e.Member.ToLower()
MethodCallExpression toLowerExp = Expression.Call(member, toLowerMethod);
//e => e.Member.Contains(value)
MethodCallExpression containsExp = Expression.Call(toLowerExp, containsMethod, constant);
//e => e.Member != null && e.Member.Contains(value)
BinaryExpression filterExpression = Expression.AndAlso(notNullExp, containsExp);
predicate = predicate == null ? (Expression)filterExpression : Expression.OrElse(predicate, filterExpression);
}
var lambda = Expression.Lambda<Func<TEntity, bool>>(predicate, parameter);
return query.Where(lambda);
}
在您的 Get 方法中,您将拥有类似的内容:
[HttpGet]
public virtual async Task<IActionResult> List([FromQuery] string filter)
{
var query = _context.Set<TEntity>();
query = Filter(query, filter);
var result = await query.ToListAsync();
return Ok(result);
}
关于c# - WebAPI - 基于实体修饰属性的通用搜索服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58939102/
我想为我的 ABP 项目中的特定应用程序服务关闭自动 WebAPI 生成服务。 最佳答案 RemoteService 属性可用于将类标记为远程服务或禁用固有地实现 IRemoteService 接口(
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我有以下 Controller ,它应该接受用户名和密码作为 POST 中的有效负载。如果我将其更改为 HttpGet 就可以了。 [RoutePrefix("api")] public class
使用以下路线: routes.MapHttpRoute( name: "Set", routeTemplate: "api/set/{id}",
我正在使用 AngularJS,我正在尝试将 json 从我的服务发送到 webAPI Controller 。当我通过发送时,我在 webApi 函数的参数中收到 null。 我的功能服务是: an
据我了解,如果我有一个 ASP.NET WebApi 方法,其签名如下所示...... public HttpResponseMessage PostCustomer(Customer custome
我遇到了一个解决方案问题,我使用 Visual Studio SPA 模板中的部分在具有 Oauth 身份验证的 WebApi 中拥有帐户 Controller 。 app.UseOAuthBea
我按照此处的说明将 webApi.HelpPage 区域和 View 添加到使用 structureMap 的现有项目中 - 但是在访问/Help url 时: StructureMap Except
我有一个 WebAPI。如何返回并打开网页。例如,我想打开 CNN.com 页面。 [HttpGet] public HttpResponseMessage Get()
我想知道是否有人可以澄清这一点。我发现用法令人困惑。 链接和视频都没有回答我的问题 我知道像这样的链接 asp.net core middleware vs filters 甚至还有关于它的视频 但是
运行以下最新版本(在撰写本文时): Visual Studio 2019 16.4.5 .NET 核心 SDK 3.1.102 x64 测试的浏览器: 谷歌浏览器 80.0.3987.122 火狐 7
想法是,将有一个外部实体 (SharePoint) 调用我的 WebAPI 并传入 PDF 文件以及有关该 PDF 文件的一些额外元数据信息。我被困在如何构造 Web API 方法的签名上。这是我到目
我有一个 WebApi 服务处理来自简单表单的上传,如下所示: 但是,我不知道如何使用 HttpClient API 模拟同一
嗨,我是 Angular 的新手,现在我从一个示例登录页面开始,该页面传递包含用户名和密码的 userEntity。userEntity 是一个对象/实体是 webapi。 问题:当我为登录按钮单击
我有一个 AngularJS + MVC + WebAPI,我正在尝试:- 使用标准(个人账户)进行MVC认证;- 使用相同的用户和密码进行基于 WebAPI 的身份验证。 问题,AngularJS
Web API 的版本存在一些混淆。看看这个 Web API at NuGet , Microsoft ASP.NET Web API 2.2 5.2.3 什么?这里是没有提到 2.2 的描述 我的猜
我正在开发一个 Web 应用程序,该应用程序将 Owin 托管用于 MVC 和 WebApi 2。 我最近将 Microsoft Mvc/WebApi 包从 5.2.2 版升级到 5.2.3 版,将
随着Web技术的发展,现在各种框架,前端的,后端的,数不胜数。全栈工程师的压力越来越大。 现在的前端的框架,既可以做各种Web,又可以做各种APP,前端框架更新换代越来越快,越来越多。 传统的模
1. WebAPI 背景知识 1.1 什么是 WebAPI JS 分成三个大的部分: ECMAScript: 基础语法部分 DOM API: 操作页面结构 BOM API: 操作浏览器 WebAPI
我是一名优秀的程序员,十分优秀!