我有以下控制器方法:
public IActionResult Get()
{
IEnumerable<Hour> _hours = _hourRepository
.AllIncluding(h => h.Customer.name, h => h.Payer.name)
.OrderByDescending(h => h.hourdate)
.ToList();
return Ok(_hours);
}
我想更改方法以使用别名,因为Customer
和Payer
都会返回相同的名称字段。
public IActionResult Get()
{
IEnumerable<Hour> _hours = _hourRepository
.AllIncluding(
h => new { customer_name = h.Customer.name },
h => new { payer_name = h.Payer.name })
.OrderByDescending(h => h.hourdate)
.ToList();
return Ok(_hours);
}
然而,这会导致错误,因为我使用不支持此功能的存储库...我认为。
属性表达式'h =&gt; new&lt;&gt; f__AnonymousType1`1(customer_name = h.Customer.name)'无效。表达式应代表属性访问:'t =&gt; t.MyProperty'
在我的存储库下面:
public virtual IEnumerable<T> AllIncluding(params Expression<Func<T,
object>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.AsEnumerable();
}
如何更改存储库方法AllIncluding
以接受上述两个选项?