简体   繁体   English

按日期排序 LINQ 查询中的时间中断

[英]Order By Date Time breaks in LINQ query

The following details the error I am getting The Student Class以下详细说明了我收到学生类的错误

public class Student () {
    public int Id {get;set;}
    public Student(int id IEnumerable<StudentOwner> owner) {
         _owners = owners != null ? owners.ToList() : new List<StudentOwner>();
    }
    private List<StudentOwner> _owners;
}

The StudentDetailsClass StudentDetailsClass

public class StudentDetails () {
    pubiic int Id {get; set;}
    public DateTime UpdateDate { get; }
    public string Comments { get; set; }
    public int StudentRef {get; set; }

}

A selection of the offending code for the query is as follows, it works if you order by s.ID查询的违规代码选择如下,如果您按 s.ID 订购,则有效

  baseQuery = baseQuery.Where(
              o => s.StudentDetails
                   .OrderByDescending(s => **s.UpdateDate**).First().StudentRef
                == StudentSearchParams.StudentRef;

The following error occurs, yet if you do order by Id it works but that does not produce the required result.发生以下错误,但如果您按 Id 订购它可以工作,但不会产生所需的结果。 Any ideas ???有任何想法吗 ???

 .OrderByDescending(s1 => s1.UpdateDate)' could not be translated. 
 Additional information: Translation of member 'UpdateDate' on entity type 'StudentDetails' failed. 
 This commonly occurs when the specified member is unmapped. Either rewrite the query in a form 
 that can be translated, or switch to client evaluation explicitly by inserting a call to 
 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

The error message is telling you错误信息告诉你

Translation of member 'UpdateDate' on entity type 'StudentDetails' failed.实体类型“StudentDetails”上成员“UpdateDate”的翻译失败。 This commonly occurs when the specified member is unmapped这通常发生在指定成员未映射时

And what you have is而你所拥有的是

public class StudentDetails
{
    public DateTime UpdateDate { get; }
}

ie get only property (no setter).只获取属性(没有设置器)。 And by EF Core conventions通过 EF Core 约定

By convention, all public properties with a getter and a setter will be included in the model.按照惯例,所有带有 getter 和 setter 的公共属性都将包含在模型中。

in other words, properties w/oa setter are ignored (not included, unmapped).换句话说,没有设置器的属性被忽略(不包括,未映射)。

Append set;追加set; after get; get;get; and the problem will be gone.问题就会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM