Home > Net >  Order By Date Time breaks in LINQ query
Order By Date Time breaks in LINQ query

Time:10-21

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

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

  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. 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.

CodePudding user response:

The error message is telling you

Translation of member 'UpdateDate' on entity type 'StudentDetails' failed. This commonly occurs when the specified member is unmapped

And what you have is

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

i.e. get only property (no setter). And by EF Core conventions

By convention, all public properties with a getter and a setter will be included in the model.

in other words, properties w/o a setter are ignored (not included, unmapped).

Append set; after get; and the problem will be gone.

  • Related