none
LIST capacity & count is getting different RRS feed

  • Question

  • when i declare list then i mention what would my list size but its capacity is getting higher means more allocation is happening which occupy memory.

    see my code

                List<MyData> JoinResult2 = new List<MyData>(5);
               JoinResult2 = (from p in dt.AsEnumerable()
                                   join t in dtTax.AsEnumerable()
                                   on p.Field<int>("Tax Id") equals t.Field<int>("Tax Id") into pj
                                   from r in pj.DefaultIfEmpty()
                                   select new MyData
                                   {
                                       ProductName = p.Field<string>("Product Name"),
                                       BrandName = p.Field<string>("Brand Name"),
                                       ProductCategory = r == null ? "" : r.Field<string>("Product Category"),
                                       TaxCharge = r == null ? 0 : r.Field<int>("Charge")
                                   }).ToList();
    
    
    its capacity is getting 8 when count is 5. how to force list to create 5 element instead of 8 ? where i made the mistake in my code?

    Sunday, August 25, 2019 6:43 PM

All replies

  • I do not believe there is a way to set the capacity to be just the results of the query ToList method.  The list with a capacity of 5 is being overwritten by the results of the linq query
    Sunday, August 25, 2019 7:40 PM
  • Hi Sudip,

    Use .Take() to force the query to only Take the number of items you want in your List:

     List<MyData> JoinResult2 = new List<MyData>(5);
               JoinResult2 = (from p in dt.AsEnumerable()
                                   join t in dtTax.AsEnumerable()
                                   on p.Field<int>("Tax Id") equals t.Field<int>("Tax Id") into pj
                                   from r in pj.DefaultIfEmpty()
                                   select new MyData
                                   {
                                       ProductName = p.Field<string>("Product Name"),
                                       BrandName = p.Field<string>("Brand Name"),
                                       ProductCategory = r == null ? "" : r.Field<string>("Product Category"),
                                       TaxCharge = r == null ? 0 : r.Field<int>("Charge")
                                   }).Take(JoinResult2.Capacity).ToList();
    It will take the first 5 items from the query and "send" them .ToList()


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, August 25, 2019 11:53 PM
    Moderator
  • does it equal the JoinResult2 list count & capacity if i use take(5) function ?
    Monday, August 26, 2019 3:04 PM
  • does it equal the JoinResult2 list count & capacity if i use take(5) function ?
    The Count was 5 when I tested it yesterday. But, I didn't look at the Capacity then. So, I just looked at it now and, unfortunately, that did *not* remain at 5, it was larger. So, maybe this will *not* work as you need it to. Sorry about that …  =0(  

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, August 26, 2019 3:26 PM
    Moderator
  • Wait ... one more thing … I tried setting the Capacity back to 5 after it had changed, and that worked. So, you just need a few extra steps (save the original Capacity in a variable, run your query, set the Capacity back to the saved value).

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, August 26, 2019 3:28 PM
    Moderator
  • Think about what your code is doing.  The LINQ expression never sees a copy of JoinResult2.  Your initial allocation is useless; the list you allocated with "new List<MyData>(5)" is immediately thrown away, and the brand new list returned from the LINQ expression is assigned to JoinResult2.  If you want that request limited to 5 entries, then you have to tell the LINQ expression to return only 5 entries.  That's what "take" is for.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, August 26, 2019 4:52 PM
  • sir i want List count & capacity should be same after returning data from LINQ query which will be storing in JoinResult2
    Tuesday, August 27, 2019 1:34 PM
  • sir i want List count & capacity should be same after returning data from LINQ query which will be storing in JoinResult2
    Unfortunately, the Capacity may differ from the Count, no matter if you do it with my suggestion or with Tim's suggestion. You will need to correct the Capacity, as I pointed out in my previous reply.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, August 27, 2019 2:49 PM
    Moderator