Bruno Rigoni
Bruno Rigoni

Reputation: 45

Max value LINQ using class

I've got this class

public class Materiale
{
    public string IdMateriale { get; set; }
    public string GenereMateriale { get; set; }
    public string Categoria { get; set; }
    public string Modello { get; set; }
    public string Tipo { get; set; }
    public string NumSerie { get; set; }
    public int Anno { get; set; }
    public string DittaCostruttrice { get; set; }
    public string Note { get; set; }
    public List<Controllo> Controlli = new List<Controllo>();
}

public class Controllo
{
    public string IdControllo { get; set; }
    public DateTime DataControllo { get; set; }
    public string IdMateriale { get; set; }
    public string Utente { get; set; }
    public string Stato { get; set; }
    public string Note { get; set; }
}

I want to query a list of "Materiale" filtering "Controlli". I need to retrieve all properties of the "Materiale" class and only one property of the "Controllo" class (the one named "Stato"). From the list "Controlli" I need the one that has the most recent "DataControllo" property.

I try this in a LINQ query but I receive an error (Max doesn't exist in the current context)

List<Materiale> m = new List<Materiale>();

List<Materiale> m2 = (from ma in m
                      from c in ma.Controlli
                      where c.DataControllo == Max(c.DataControllo)                                  
                      select new
                      {
                           ma, c.Stato
                      }).ToList();

Can someone help me

Upvotes: 2

Views: 64

Answers (2)

Christos
Christos

Reputation: 53958

I think that you need something more simple like the following one:

List<Materiale> m2 = from ma in m
                     let mostRecentControllo = ma.Controlli 
                                                 .OrderByDescending(c=>c.DataControllo)
                                                 .FirstOrDefault()
                     select new
                     {
                         Materiale = ma, 
                         Stato = mostRecentControllo != null
                                ? mostRecentControllo.Stato : null
                     }).ToList();

I have supposed that each Materiale's Controlli list contains Controllo with the same IdMateriale.

Upvotes: 1

Rahul Singh
Rahul Singh

Reputation: 21795

@Christos is correct, here is my version with let in query syntax:-

List<Materiale> m2 = from m in MaterialeList
                     let RecentControllo = m.OrderByDescending(x => x.DataControllo)
                                            .FirstOrDefault()
                     select new Materiale
                     {
                         IdMateriale = m.IdMateriale,
                         GenereMateriale = m.GenereMateriale,
                         //Similarily other properties of  Materiale here
                         Stato = RecentControllo != null ? RecentControllo.Stato : ""
                     }).ToList();

Upvotes: 1

Related Questions