Fabrice Mainguené
Fabrice Mainguené

Reputation: 506

How find Max in a query with a join

I created this query, and I don't know how find the last date_attribution

var usertmp = from user in entity.inv_utilisateur
              join userbadgetmp in entity.inv_utilisateur_badge on user.u_id equals userbadgetmp.u_id into gj
              from userbadge in gj.DefaultIfEmpty()

              select new UserEJ
              {
                  u_id = (int) user.u_id,
                  name = user.name,
                  date_attrib =  userbadge.date_attribution // here I want find the last date
              };

for exemple if I have this 2 tables:

inv_utilisateur                                inv_utilisateur_badge
u_id          name                               u_id           date_attribution
1             name1                              1              20130911
                                                 1              20130807
2             name2                              2              20120608
3             name3 

I need the result

u_id               name             date_attribution
1                  name1            20130911
2                  name2            20120608
3                  name3            

Edit 1

the type of the field are : u_id : int, name : string, date_attribution : datetime

Upvotes: 0

Views: 48

Answers (2)

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

var usertmp = from user in entity.inv_utilisateur
              join userbadgetmp in entity.inv_utilisateur_badge on user.u_id equals userbadgetmp.u_id into gj
              from userbadge in gj.DefaultIfEmpty()
              group userbadge by new{user.u_id, user.name} into g
              select new UserEJ {
                 u_id = (int)g.Key.u_id,
                 name = g.Key.name,
                 date_attrib = g.Max(x => x!= null ? x.date_attribution : <a defaultvalue>)
              }

you could also do

var usertmp = from user in entity.inv_utilisateur
              let max_dt = (from userbadge in entity.inv_utilisateur_badge
                            where user.u_id == userbadge.u_id
                            select userbadge.date_attribution).Max()//??someDefaultvalue if you don't want null
              select new UserEJ {
                 u_id = user.u_id,
                 name = g.name,
                 date_attrib = max_dt
              }

Upvotes: 1

Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18411

Sorry for my answer but i am not used in the from clause:

var usertmp = entity.inv_utilisateur
    .Join
    (
        entity.inv_utilisateur_badge,
        x=>x.u_id,
        x=>x.u_id,
        (user,userbadge) => new 
        {
            u_id = user.u_id, 
            name = user.name, 
            date_attrib = userbadge.date_attribution
        }
    )
    .GroupBy(x=>new {x.u_id,x.name})
    .Select
    (
        x => new 
        {
            x.Key.u_id,
            x.Key.name,
            date_attrib = x.Max(z=>z.date_attrib)
        }
    );

Upvotes: 1

Related Questions