CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: LINQ sumWhere

Threaded View

  1. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: LINQ sumWhere

    Seems I worked it out....

    Code:
    _myArray.Sum(a => a.Equals(n) ? a : 0);
    or in my case which has an array of objects with an Int32 Value member.

    Code:
    _myObjectArray.Sum(o => o.Value.Equals(n) ? o.Value : 0);
    Is there a neater way?

    On another point... does anyone else use the tertiary operator and find that 90% of the time, one of the then/else values is the same as the if value?

    Eg (upperbound of a value):

    return (a > 25 ? 25 : a);

    I always think there should be a binary operator equivelent, so you could remove the ": a" from the end of the above.

    Eg:

    return (a > 25 ? 25);

    should do the same. From my C -> C++ -> C# days, I've always thought this. Maybe my coding style... I don't know...
    Last edited by rliq; August 19th, 2009 at 01:33 AM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured