adding properties together from list of objects using LINQ

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












Looking to see if there is a simpler way to achieve this. I would love if I could keep the return as a one-line LINQ expression. I want to grab the total number of hints used, which can be more than one.



public int hintsUsed

get

int i = 0;
questions.ForEach(q => i += q.hintsUsed);
return i;








share|improve this question



























    up vote
    0
    down vote

    favorite












    Looking to see if there is a simpler way to achieve this. I would love if I could keep the return as a one-line LINQ expression. I want to grab the total number of hints used, which can be more than one.



    public int hintsUsed

    get

    int i = 0;
    questions.ForEach(q => i += q.hintsUsed);
    return i;








    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Looking to see if there is a simpler way to achieve this. I would love if I could keep the return as a one-line LINQ expression. I want to grab the total number of hints used, which can be more than one.



      public int hintsUsed

      get

      int i = 0;
      questions.ForEach(q => i += q.hintsUsed);
      return i;








      share|improve this question













      Looking to see if there is a simpler way to achieve this. I would love if I could keep the return as a one-line LINQ expression. I want to grab the total number of hints used, which can be more than one.



      public int hintsUsed

      get

      int i = 0;
      questions.ForEach(q => i += q.hintsUsed);
      return i;










      share|improve this question












      share|improve this question




      share|improve this question








      edited May 13 at 20:55









      Heslacher

      43.9k359152




      43.9k359152









      asked May 13 at 20:53









      Premier Bromanov

      1135




      1135




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You can use the Sum function



          public int HintsUsed => questions.Sum(q => q.hintsUsed);


          By using an expression bodied member (since C# 6.0), you can even get rid of the get and the return keywords plus a few braces.



          The code above is equivalent to



          public int HintsUsed

          get

          return questions.Sum(q => q.hintsUsed);




          Most of the LINQ-to-Objects functionality is provided by the Enumerable Class.






          share|improve this answer























          • Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
            – Premier Bromanov
            May 13 at 21:15










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194318%2fadding-properties-together-from-list-of-objects-using-linq%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          You can use the Sum function



          public int HintsUsed => questions.Sum(q => q.hintsUsed);


          By using an expression bodied member (since C# 6.0), you can even get rid of the get and the return keywords plus a few braces.



          The code above is equivalent to



          public int HintsUsed

          get

          return questions.Sum(q => q.hintsUsed);




          Most of the LINQ-to-Objects functionality is provided by the Enumerable Class.






          share|improve this answer























          • Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
            – Premier Bromanov
            May 13 at 21:15














          up vote
          6
          down vote



          accepted










          You can use the Sum function



          public int HintsUsed => questions.Sum(q => q.hintsUsed);


          By using an expression bodied member (since C# 6.0), you can even get rid of the get and the return keywords plus a few braces.



          The code above is equivalent to



          public int HintsUsed

          get

          return questions.Sum(q => q.hintsUsed);




          Most of the LINQ-to-Objects functionality is provided by the Enumerable Class.






          share|improve this answer























          • Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
            – Premier Bromanov
            May 13 at 21:15












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          You can use the Sum function



          public int HintsUsed => questions.Sum(q => q.hintsUsed);


          By using an expression bodied member (since C# 6.0), you can even get rid of the get and the return keywords plus a few braces.



          The code above is equivalent to



          public int HintsUsed

          get

          return questions.Sum(q => q.hintsUsed);




          Most of the LINQ-to-Objects functionality is provided by the Enumerable Class.






          share|improve this answer















          You can use the Sum function



          public int HintsUsed => questions.Sum(q => q.hintsUsed);


          By using an expression bodied member (since C# 6.0), you can even get rid of the get and the return keywords plus a few braces.



          The code above is equivalent to



          public int HintsUsed

          get

          return questions.Sum(q => q.hintsUsed);




          Most of the LINQ-to-Objects functionality is provided by the Enumerable Class.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 15 at 11:23


























          answered May 13 at 21:01









          Olivier Jacot-Descombes

          2,3611016




          2,3611016











          • Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
            – Premier Bromanov
            May 13 at 21:15
















          • Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
            – Premier Bromanov
            May 13 at 21:15















          Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
          – Premier Bromanov
          May 13 at 21:15




          Ahh perfect, thank you. Sometimes its difficult to search for these things because I'm not sure which words to use. I will definitely use this in the future.
          – Premier Bromanov
          May 13 at 21:15












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194318%2fadding-properties-together-from-list-of-objects-using-linq%23new-answer', 'question_page');

          );

          Post as a guest













































































          WgogBzqGoetopmd,Al6Dp 1kaH9
          wlNWcr,DtFkLRdgnkp7agOGS,ZLdwt xhpniVG1TMxyAtmvDHw67AV813,UCeFb S KhpJ,yZfEZ,U l1XKKH

          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Python - Quiz Game with Tkinter