Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Create a Collection in C#

Create a Collection in C#

Scheduled Pinned Locked Moved C#
databasecsharpdata-structuresquestion
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M macca24

    I want to do this through C# though

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #8

    The just add the collection items together! Use a foreach loop, or even a Linq method:

    var sum = collection.Sum(item => item);

    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    M 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      The just add the collection items together! Use a foreach loop, or even a Linq method:

      var sum = collection.Sum(item => item);

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      M Offline
      M Offline
      macca24
      wrote on last edited by
      #9

      Linq Method Would probably be the best as I have to write it out to a field. I'll put together some code and see what you think?

      OriginalGriffO 1 Reply Last reply
      0
      • M macca24

        Linq Method Would probably be the best as I have to write it out to a field. I'll put together some code and see what you think?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #10

        Sounds good!

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        M 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Sounds good!

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          M Offline
          M Offline
          macca24
          wrote on last edited by
          #11

          Im trying to do something like this: var complexQuery = from e in orgContext.CreateQuery("Eval") join c in orgContext.CreateQuery("Contract") on e["contractid"] equals c["contractnameid"] I want to do a Count of the complexQuery and whatever the count is I want to add this to a variable so I can write this out to a field.

          OriginalGriffO 1 Reply Last reply
          0
          • M macca24

            Im trying to do something like this: var complexQuery = from e in orgContext.CreateQuery("Eval") join c in orgContext.CreateQuery("Contract") on e["contractid"] equals c["contractnameid"] I want to do a Count of the complexQuery and whatever the count is I want to add this to a variable so I can write this out to a field.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #12

            Um...you do realize that a Collection has a Count method?[^] So to get all the elements in your total sequence:

            var complexQuery = from e in orgContext.CreateQuery("Eval")
            join c in orgContext.CreateQuery("Contract")
            on e["contractid"] equals c["contractnameid"];
            int count = complexQuery.Count();

            If you are trying to get the count of elements with matching ids (i.e. a collection of counts) then just Group the linq query and select the count of each group.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            M 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Um...you do realize that a Collection has a Count method?[^] So to get all the elements in your total sequence:

              var complexQuery = from e in orgContext.CreateQuery("Eval")
              join c in orgContext.CreateQuery("Contract")
              on e["contractid"] equals c["contractnameid"];
              int count = complexQuery.Count();

              If you are trying to get the count of elements with matching ids (i.e. a collection of counts) then just Group the linq query and select the count of each group.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              M Offline
              M Offline
              macca24
              wrote on last edited by
              #13

              Thanks for your help Ill try this.

              OriginalGriffO 1 Reply Last reply
              0
              • M macca24

                Thanks for your help Ill try this.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #14

                You're welcome!

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • M macca24

                  I have a table in a database containing a number of records. The records have an ID number i.e. H1, H2, H3. Each record also contains a field called C1 which contains a value. There can be multiple records with the same ID. I want to read all the values of fields C1 for records with the ID H1 into an array. I don't know exactly how many records there are with the ID H1. I don't want to do this in SQL. Does anyone know how I would create a collection holding the values?

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #15

                  macca24 wrote:

                  I don't want to do this in SQL.

                  Why not? If the set is small, like say less than 100,000 than a query each time is likely to be best. If the set is large, like 10 billion, then dragging all of the records across the wire probably isn't a good idea. Which suggests you basic assumption of a solution is wrong and again SQL would probably be better for a real solution.

                  M 1 Reply Last reply
                  0
                  • J jschell

                    macca24 wrote:

                    I don't want to do this in SQL.

                    Why not? If the set is small, like say less than 100,000 than a query each time is likely to be best. If the set is large, like 10 billion, then dragging all of the records across the wire probably isn't a good idea. Which suggests you basic assumption of a solution is wrong and again SQL would probably be better for a real solution.

                    M Offline
                    M Offline
                    macca24
                    wrote on last edited by
                    #16

                    It is only for a very small number of records around 50-100.

                    J 1 Reply Last reply
                    0
                    • M macca24

                      It is only for a very small number of records around 50-100.

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #17

                      Then as I suggested - do the query.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups