我正在使用示例here用工作单元创建存储库模式。
在代码中,有一个通用的Get方法:
public class GenericRepository<TEntity> where TEntity : class
{
internal AdminDbContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(AdminDbContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<TEntity, TEntity> selector = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
此时,调用此方法时,将从数据库中获取所有记录,并在内存中完成列选择。
我的问题是如何在此处扩展Get方法,使我可以将列名称动态传递给此方法,以便在数据库级别仅选择所需的列?
我试过了:
接受带有所需列的逗号分隔值的字符串参数,但是我无法将它们映射到实体。
使用相同类型的过滤器创建参数,该参数会导致错误。
最佳答案
这是一个很长的答案,但这是我为此创建的扩展方法。在这种情况下,我将返回一个对象,因为该对象在webAPI中仅用于返回json,而我不需要特定的类型,但是可以轻松地将其修改为返回通用实体类型。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
namespace Your.Extensions
{
public enum PropertyFormat
{
AsIs,
PascalCase,
CamelCase
}
public static class DataShapingExtensions
{
public static object ToDataShape<ObjectIn>(this ObjectIn objectToShape, string fields, PropertyFormat propertyFormat = PropertyFormat.AsIs) where ObjectIn : class
{
var listOfFields = new List<string>();
if (!string.IsNullOrWhiteSpace(fields))
{
listOfFields = fields.ToLower().Split(',').ToList();
}
if (listOfFields.Any())
{
var objectToReturn = new JObject();
//====
var enumerable = objectToShape as IEnumerable;
if (enumerable != null)
{
var listOfObjects = new List<JObject>();
foreach (var item in enumerable)
{
var objectToReturn2 = new JObject();
listOfFields.ForEach(field =>
{
try
{
var prop = item.GetType()
.GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var fieldName = prop.Name;
var fieldValue = prop.GetValue(item, null);
fieldName = GetName(fieldName, propertyFormat);
objectToReturn2.Add(new JProperty(fieldName, fieldValue));
}
catch (Exception ex) { }
});
listOfObjects.Add(objectToReturn2);
}
return listOfObjects.ConvertAll(o => o);
}
//====
listOfFields.ForEach(field =>
{
try
{
var prop = objectToShape.GetType()
.GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var fieldName = prop.Name;
var fieldValue = prop.GetValue(objectToShape, null);
fieldName = GetName(fieldName, propertyFormat);
objectToReturn.Add(new JProperty(fieldName, fieldValue));
}
catch (Exception ex) { }
});
return objectToReturn;
}
return objectToShape;
}
private static string GetName(string field, PropertyFormat propertyFormat)
{
switch (propertyFormat)
{
case PropertyFormat.AsIs: return field;
case PropertyFormat.PascalCase: return field.ToPascalCase();
case PropertyFormat.CamelCase: return field.ToCamelCase();
default: return field;
}
}
}
}
//用法
[HttpGet, Route("api/someroute")]
public async Task<IHttpActionResult> YourMethod(string fields = null)
{
try
{
var products = await yourRepo.GetProductsList();
if (fields.HasValue())
{
return Ok(products.ToDataShape(fields, PropertyFormat.CamelCase));
}
return Ok(products);
}
catch (Exception)
{
return InternalServerError();
}
}
//通话路线
/api/someroute?fields=productID,productName
//输出(json)
{
productID: 1,
productName: "Some Name"
}
关于c# - 信息库模式仅检索所需的列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36547702/