locked
should we put order by in list of objects while returning form api or they keep the order in which they are created. RRS feed

  • Question

  • User2102072086 posted

    public class pdate
    {

    public DateTime ProgressDate { get; set; }

    public prog decimal{get;set;]

    }

    List<pdate> listpdate = new List<pdate>();
    SCurve scurveObj = null;

    DateTime dtFrom1 = DateTime.SpecifyKind(Convert.ToDateTime("01 jan 2019").Date, DateTimeKind.Utc);
    DateTime dtTo1 = DateTime.SpecifyKind(Convert.ToDateTime("01 feb 2019").Date, DateTimeKind.Utc);

    for (DateTime i = dtFrom1; i <= dtTo1; i = i.AddDays(1))
    {
    pdate = new pdate();

    pdate.ProgressDate = i.Date;

    pdate.prog = 10;

    listpdate.Add(pdate);

    }

    this code is in api which returns listpdate;

    Q1) in return stmt should i put order by or will it keep the order in which i have inserted the objects, and finally the api is called in .js so will get every thing in the same order

    in which i have entered.

    return listpdate.OrderBy(o => o.ProgressDate).ToList();

    q2) if i group by on one col in return stmt , then should we use order by or it will keep the objects in listpdate ordered by progressdate in the order i have entered in loop.

    so in simple should i put ordeby or not in this case. where i need the data at .js on page order by progress date.

     return listpdate.GroupBy(val => DateTime.SpecifyKind(new DateTime(val.ProgressDate.Year, val.ProgressDate.Month,
                                                         DateTime.DaysInMonth(val.ProgressDate.Year, val.ProgressDate.Month)),DateTimeKind.Utc))
                        .Select(grouped => new pdate()
                        {
                            ProgressDate = grouped.Key
                           ,
                            prog = grouped.Sum(Pp => Pp.prog)
                           }).OrderBy(o => o.ProgressDate).ToList();
     
    Friday, April 10, 2020 7:52 AM

All replies

  • User475983607 posted

    Q1) in return stmt should i put order by or will it keep the order in which i have inserted the objects, and finally the api is called in .js so will get every thing in the same order

    The List<T> items are in the order that you added the items.  

    so in simple should i put ordeby or not in this case. where i need the data at .js on page order by progress date.

    Order has no meaning in set theory.  Therefore any SQL (LINQ) needs to be ordered if order is importation to your design.

    Please do a better job formatting your posts.   Use the "Insert/Edit code samples" button {;} when sharing source code.

    Friday, April 10, 2020 11:11 AM
  • User2102072086 posted
     return listpdate.GroupBy(val => DateTime.SpecifyKind(new DateTime(val.ProgressDate.Year, val.ProgressDate.Month,                                                    DateTime.DaysInMonth(val.ProgressDate.Year, val.ProgressDate.Month)),DateTimeKind.Utc))                   .Select(grouped => new pdate()                  {                       ProgressDate = grouped.Key,prog = grouped.Sum(Pp => Pp.prog)                      }).OrderBy(o => o.ProgressDate).ToList();


    my second q2) is unanswered.

    Saturday, April 11, 2020 1:12 PM
  • User475983607 posted

    No.  Your question was answered directly.  For the second time, Query results must be ordered if order matters in your design.  Otherwise the order is not deterministic.  I cannot speak to your design as you have not explained why order matters.  I assume you are trying to sort a list or collection .

    Again, please make an effort to format your code when posting on the forums.  It's irritating when you force the community to copy your code, paste it, then format the codein order to review the code. 

    Saturday, April 11, 2020 1:23 PM
  • User2102072086 posted

    can i get the link of msdn where microsoft has written that,

    the list of objects(List<T>)   keeps the order of object inside the list in which they were added?

    Monday, April 13, 2020 11:59 AM
  • User475983607 posted

    rajemessage

    can i get the link of msdn where microsoft has written that,

    the list of objects(List<T>)   keeps the order of object inside the list in which they were added?

    This rule applies as long as you do not modify the List<T>.  As soon as you query, sort, insert, etc then the original order is not garunteed.  

    The List<T> is not guaranteed to be sorted. You must sort the List<T> before performing operations (such as BinarySearch) that require the List<T> to be sorted.

    In other words, List<T> is not modified unless you - the programmer - does something to the list.  For example, if you create a List<int> { 2, 9, 6 ,8},  the .NET framework is not going to perform and extra operation on the List<int>.

    The reference documentation is not hidden.  

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8

    Monday, April 13, 2020 12:59 PM