none
Linq sum of multiple fields

    Question

  • Hello friends,

    I am new to Linq...

    How can i write a query in linq with multiple sum fields in VB.net.

     

    here is sql.

    select sum(fl1),sum(fl2),sum(fl3)

    where fl1 =cn1 and fl2=cn2

     

    also another one with group by

     

    select g1,sum(fl1),sum(fl2),sum(fl3)

    where fl1 =cn1 and fl2=cn2

    group by g1

     

     

    Thanks

    Matt

     

    Friday, July 30, 2010 3:38 PM

Answers

  • select sum(fl1),sum(fl2),sum(fl3)
    
    where fl1 =cn1 and fl2=cn2

    will change to

    Dim x = From p in db.YourClass _
    
    Where p.fl1 = cn1 And p.f12 = cn2
    
    Select _
    
    sum1 = x.Sum(Function(y) y.fl1), _
    
    sum2 = x.Sum(Function(y) y.fl2), _
    
    sum3 = x.Sum(Function(y) y.fl3)
    
    
    

     

     

    select g1,sum(fl1),sum(fl2),sum(fl3)
    
    where fl1 =cn1 and fl2=cn2
    
    group by g1

     

    will change into

    Dim x = From p In db.YourClass _
      Where p.fl1 = cn1 And p.f12 = cn2
                Group p By p.g1 _
                Into Group _             Select _             g1, _             Sum1 = Group.Sum(Function(y) y.fl1), _             Sum2 = Group.Sum(Function(y) y.fl2), _             Sum3 = Group.Sum(Function(y) y.fl3)

     

     


    Ali Hamdar (alihamdar.com)
    Sunday, August 01, 2010 1:24 AM