How to group a table by a certain attribute then count those elements that are present in an other table...












-2















Let's say I have two tables (entities):



class Person
{
public int Id { get; set; } // primary key
public string City { get; set; } // the attribute to group by
}

class JoinTable
{
public int Id { get; set; } // primary key
public int Person_Id { get; set; } // foreign key referencing a Person entity/row

public int SomeOther_Id { get; set; } // foreign key referencing some other irrelevant entity/row
}


I want to group all Person entities by their "City" attribute and count how many people are referenced in the JoinTable by each city.
How do I query that in LINQ?










share|improve this question





























    -2















    Let's say I have two tables (entities):



    class Person
    {
    public int Id { get; set; } // primary key
    public string City { get; set; } // the attribute to group by
    }

    class JoinTable
    {
    public int Id { get; set; } // primary key
    public int Person_Id { get; set; } // foreign key referencing a Person entity/row

    public int SomeOther_Id { get; set; } // foreign key referencing some other irrelevant entity/row
    }


    I want to group all Person entities by their "City" attribute and count how many people are referenced in the JoinTable by each city.
    How do I query that in LINQ?










    share|improve this question



























      -2












      -2








      -2








      Let's say I have two tables (entities):



      class Person
      {
      public int Id { get; set; } // primary key
      public string City { get; set; } // the attribute to group by
      }

      class JoinTable
      {
      public int Id { get; set; } // primary key
      public int Person_Id { get; set; } // foreign key referencing a Person entity/row

      public int SomeOther_Id { get; set; } // foreign key referencing some other irrelevant entity/row
      }


      I want to group all Person entities by their "City" attribute and count how many people are referenced in the JoinTable by each city.
      How do I query that in LINQ?










      share|improve this question
















      Let's say I have two tables (entities):



      class Person
      {
      public int Id { get; set; } // primary key
      public string City { get; set; } // the attribute to group by
      }

      class JoinTable
      {
      public int Id { get; set; } // primary key
      public int Person_Id { get; set; } // foreign key referencing a Person entity/row

      public int SomeOther_Id { get; set; } // foreign key referencing some other irrelevant entity/row
      }


      I want to group all Person entities by their "City" attribute and count how many people are referenced in the JoinTable by each city.
      How do I query that in LINQ?







      c# linq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 17:40









      kara

      1,86431124




      1,86431124










      asked Jan 2 at 15:30









      L.Z.L.Z.

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I'm not quite sure, what you want to acchieve. But i think something like this:



          // Example Data (would be great if you could write some in your questions..)
          List<Person> ps = new List<Person>()
          {
          new Person() { Id = 1, City = "Cologne" },
          new Person() { Id = 2, City = "Cologne" },
          new Person() { Id = 3, City = "Berlin" },
          new Person() { Id = 4, City = "Berlin" },
          };
          List<JoinTable> join = new List<JoinTable>()
          {
          new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
          new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
          new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
          new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
          new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
          new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
          };

          // Join the Table and select a new object.
          var tmp = ps.Join(
          join, // which table/list should be joined
          o => o.Id, // key of outer list
          i => i.Person_Id, // key of inner list
          (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

          // now we can group out new objects
          var groupingByCity = tmp.GroupBy(g => g.City);

          // let's see what we got..
          foreach (var g in groupingByCity)
          {
          Console.WriteLine(g.Key + ": " + g.Count());

          foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
          {
          Console.WriteLine(" " + g2.Key + ": " + g2.Count());
          }
          }





          share|improve this answer























            Your Answer






            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: "1"
            };
            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009013%2fhow-to-group-a-table-by-a-certain-attribute-then-count-those-elements-that-are-p%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I'm not quite sure, what you want to acchieve. But i think something like this:



            // Example Data (would be great if you could write some in your questions..)
            List<Person> ps = new List<Person>()
            {
            new Person() { Id = 1, City = "Cologne" },
            new Person() { Id = 2, City = "Cologne" },
            new Person() { Id = 3, City = "Berlin" },
            new Person() { Id = 4, City = "Berlin" },
            };
            List<JoinTable> join = new List<JoinTable>()
            {
            new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
            new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
            new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
            new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
            new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
            new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
            };

            // Join the Table and select a new object.
            var tmp = ps.Join(
            join, // which table/list should be joined
            o => o.Id, // key of outer list
            i => i.Person_Id, // key of inner list
            (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

            // now we can group out new objects
            var groupingByCity = tmp.GroupBy(g => g.City);

            // let's see what we got..
            foreach (var g in groupingByCity)
            {
            Console.WriteLine(g.Key + ": " + g.Count());

            foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
            {
            Console.WriteLine(" " + g2.Key + ": " + g2.Count());
            }
            }





            share|improve this answer




























              0














              I'm not quite sure, what you want to acchieve. But i think something like this:



              // Example Data (would be great if you could write some in your questions..)
              List<Person> ps = new List<Person>()
              {
              new Person() { Id = 1, City = "Cologne" },
              new Person() { Id = 2, City = "Cologne" },
              new Person() { Id = 3, City = "Berlin" },
              new Person() { Id = 4, City = "Berlin" },
              };
              List<JoinTable> join = new List<JoinTable>()
              {
              new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
              new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
              new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
              new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
              new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
              new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
              };

              // Join the Table and select a new object.
              var tmp = ps.Join(
              join, // which table/list should be joined
              o => o.Id, // key of outer list
              i => i.Person_Id, // key of inner list
              (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

              // now we can group out new objects
              var groupingByCity = tmp.GroupBy(g => g.City);

              // let's see what we got..
              foreach (var g in groupingByCity)
              {
              Console.WriteLine(g.Key + ": " + g.Count());

              foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
              {
              Console.WriteLine(" " + g2.Key + ": " + g2.Count());
              }
              }





              share|improve this answer


























                0












                0








                0







                I'm not quite sure, what you want to acchieve. But i think something like this:



                // Example Data (would be great if you could write some in your questions..)
                List<Person> ps = new List<Person>()
                {
                new Person() { Id = 1, City = "Cologne" },
                new Person() { Id = 2, City = "Cologne" },
                new Person() { Id = 3, City = "Berlin" },
                new Person() { Id = 4, City = "Berlin" },
                };
                List<JoinTable> join = new List<JoinTable>()
                {
                new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
                new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
                new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
                new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
                new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
                new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
                };

                // Join the Table and select a new object.
                var tmp = ps.Join(
                join, // which table/list should be joined
                o => o.Id, // key of outer list
                i => i.Person_Id, // key of inner list
                (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

                // now we can group out new objects
                var groupingByCity = tmp.GroupBy(g => g.City);

                // let's see what we got..
                foreach (var g in groupingByCity)
                {
                Console.WriteLine(g.Key + ": " + g.Count());

                foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
                {
                Console.WriteLine(" " + g2.Key + ": " + g2.Count());
                }
                }





                share|improve this answer













                I'm not quite sure, what you want to acchieve. But i think something like this:



                // Example Data (would be great if you could write some in your questions..)
                List<Person> ps = new List<Person>()
                {
                new Person() { Id = 1, City = "Cologne" },
                new Person() { Id = 2, City = "Cologne" },
                new Person() { Id = 3, City = "Berlin" },
                new Person() { Id = 4, City = "Berlin" },
                };
                List<JoinTable> join = new List<JoinTable>()
                {
                new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
                new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
                new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
                new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
                new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
                new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
                };

                // Join the Table and select a new object.
                var tmp = ps.Join(
                join, // which table/list should be joined
                o => o.Id, // key of outer list
                i => i.Person_Id, // key of inner list
                (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

                // now we can group out new objects
                var groupingByCity = tmp.GroupBy(g => g.City);

                // let's see what we got..
                foreach (var g in groupingByCity)
                {
                Console.WriteLine(g.Key + ": " + g.Count());

                foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
                {
                Console.WriteLine(" " + g2.Key + ": " + g2.Count());
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 16:06









                karakara

                1,86431124




                1,86431124
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009013%2fhow-to-group-a-table-by-a-certain-attribute-then-count-those-elements-that-are-p%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith