Unable to sort with property name in LINQ OrderBy

后端 未结 3 1398
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 20:55

Error is

LINQ to Entities does not recognize the method \'System.Object GetValue(System.Object, System.Object[])\' method, and this method ca

3条回答
  •  一生所求
    2020-12-14 21:55

    You could try to do this using the (somewhat old) Dynamic LINQ library:

    var data = entity.User_Details
        .Take(count)
        .OrderBy(sortcriteria)
        .Skip(tblsize)
        .ToList();
    

    Alternatively, you can still sort the sequence using your original query by moving the objects into memory first, since the LINQ to Entities provider can't translate calls to the Reflection API into SQL:

    var data = entity.User_Details
        .Take(count)
        .Skip(tblsize)
        .AsEnumerable()
        .OrderBy(i => i.GetType().GetProperty(sortcriteria).GetValue(i, null))
    

提交回复
热议问题