none
Return number of Child Rows RRS feed

  • Question

  • Lets say i have a class that is populated...

    OrderNumber (Top Level)

    CustomerNumber (Top Level)

       Part (sub level)

    What is the syntax for returning the number of Part Rows ?

    Tuesday, December 16, 2014 3:41 PM

Answers

  • Same thing :

    since List<T> and array are both IEnumerable<T>, this is the same code! :) Linq is really a wonderfull thing in my opinion!


    Philippe

    Tuesday, December 16, 2014 4:09 PM

All replies

  • Hi,

    can you be more precise in your question? I don't understand what you mean by "a class that is populated"... And the "ordernumber", "customernumber" and "Part", are they class properties?


    Philippe

    Tuesday, December 16, 2014 3:51 PM
  • Ok sorry...

    assume these classes

    assume an object Cars has been instantiated and 1 car has been added...

    How can you determine how many Location rows were added ? 


            public class Cars
            {
                public string model { get; set; }

                public string color { get; set; }

                public List<Location> locate { get; set; }
            }

            public class Location
            {

                public string citycode { get; set; }

                public int qty { get; set; }
               

            }

    Tuesday, December 16, 2014 3:56 PM
  • You could just access the Count property of the List<Location>:

    Cars car = new Cars();
    //....
    int count = cars.locate.Count;

    Or if you have a list of cars:

          List<Cars> cars = new List<Cars>();
          //populate list...
          int locations = 0;
          foreach (Cars car in cars) {
            if(car.locate != null)
              locations += car.locate.Count;
          }


    Please remember to mark helpful posts as answer and/or helpful.


    Tuesday, December 16, 2014 4:02 PM
  • Well if I understood, you want to count the total number of locations from a list of cars, is that it?

    If so, you cound do this easily with Linq :

    List<Cars> lstOfCars = new List<Cars>();
    
    //Adding cars and so on...
    
    //Using "easy" Linq
    int nbLocationsOfCars = (from car in lstOfCars
                             from loc in car.locate
                             select loc.qty).Sum();
    
    //Using "raw" Linq
    int nbLocationsOfCars2 = lstOfCars.SelectMany(car => car.locate).Select(loc => loc.qty).Sum();
    

    Hope I didn't make any mistake...

    Well, it also assume that your "locate" member is not null, otherwise, we have to add one more condition :

    int nbLocationsOfCars2 = lstOfCars.Where(car => car.locate!= null).SelectMany(car => car.locate).Select(loc => loc.qty).Sum();

    This is another good example for Linq I think...


    Philippe

    Tuesday, December 16, 2014 4:06 PM
  • Thanks,

    Suppose the class were built as follows...

     public class Cars
            {
                public string model { get; set; }

                public string color { get; set; }

               public Location [] Locations
            }

            public class Location
            {

                public string citycode { get; set; }

                public int qty { get; set; }
               

            }

    Tuesday, December 16, 2014 4:07 PM
  • Just change Count to Length then:

    int count = cars.locate.Length;
    
    
          List<Cars> cars = new List<Cars>();
          //populate list...
          int locations = 0;
          foreach (Cars car in cars) {
            if(car.locate != null)
              locations += car.locate.Length;
          }
    

    Once again, please remember to mark helpful posts as answer and/or helpful.
    Tuesday, December 16, 2014 4:09 PM
  • Same thing :

    since List<T> and array are both IEnumerable<T>, this is the same code! :) Linq is really a wonderfull thing in my opinion!


    Philippe

    Tuesday, December 16, 2014 4:09 PM