Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 通过表达式创建订单<;Func<;T>&燃气轮机;具有多个字段_Linq_Entity Framework Core_Linq To Entities_Entity Framework 5 - Fatal编程技术网

Linq 通过表达式创建订单<;Func<;T>&燃气轮机;具有多个字段

Linq 通过表达式创建订单<;Func<;T>&燃气轮机;具有多个字段,linq,entity-framework-core,linq-to-entities,entity-framework-5,Linq,Entity Framework Core,Linq To Entities,Entity Framework 5,如何创建包含多个字段的order by表达式 这不是正确的语法: Expression<Func<Employee, Object>> orderByExpression = null; orderByExpression = e => e.LastName || e.FirstName; _service.GetEmployeesAsync(orderByExpression); Expression orderByExpression=null; order

如何创建包含多个字段的order by表达式

这不是正确的语法:

Expression<Func<Employee, Object>> orderByExpression = null;
orderByExpression = e => e.LastName || e.FirstName;

_service.GetEmployeesAsync(orderByExpression);
Expression orderByExpression=null;
orderByExpression=e=>e.LastName | | e.FirstName;
_GetEmployeesAsync(orderByExpression);

假设名称是字符串,这应该有效:

orderByExpression = e => e.LastName + e.FirstName;
但通过EF按多个字段排序的常见模式是使用以下1..n
然后by
调用链接
OrderBy
,因此可能更明确的方法是更改
GetEmployeesAsync
以接受
params
表达式作为参数并相应地处理它们。大概是这样的:

public Task<IEnumerable<Employee>> GetEmployeesAsync(Expression<Func<Employee, object>> orderBy, params Expression<Func<Employee, object>>[] thenBys)
{
    IQueryable<Employee> query = ...;
    var orderedQuery = query.OrderBy(orderBy);
    for (int i = 0; i < thenBys.Length; i++)
    {
        orderedQuery = orderedQuery.ThenBy(thenBys[i]);
    }
    ...
}
 _service.GetEmployeesAsync(x => x.SomeProp, x=> x.SomeOtherProp)