Home > Back-end >  Sorting and grouping a list by order number and created date
Sorting and grouping a list by order number and created date

Time:11-23

I had stuck on how to sort a list into a new list with diffirent sturcture, would if someone could help me with this issue, Thank you in advance. List with type


public class PartOrders
        {
            public string orderNumber{ get; set; }
            public DateTime createdDate { get; set; }
            public string description { get; set; }
            public string name{ get; set; }
            public DateTime performedDateTime { get; set; }
            public Catigory catigory { get; set; }
        }
public class Catigory 
        {
            public string type{ get; set; }
            public string Name{ get; set; }
        }


in the list there are many item with same orderNumber and createdDate

my question is how to make new list with type :

 

     public class PartOrders
        {
           public string orderNumber{ get; set; }
           public DateTime createdDate { get; set; }
           public List<Data> orderDate { get; set; }
        } 
public class Data
        {
            public string description { get; set; }
            public string name{ get; set; }
            public DateTime performedDateTime { get; set; }
            public Catigory catigory { get; set; }
        }


i apprisiate your help

i tried with readinonal way looping with foreach and if condition but it was very comlicated and slow result will be with this format

 

CodePudding user response:

it seems your "Lists" are not really lists but data objects or structs. First thing i would do is to create an interface for both objects that they must implement. Then you can just

List1<ICommonInterface>.Concat(List2<ICommonInterface>).OrderBy(etc..).ThenBy(etc..)

both of them.

CodePudding user response:

You'd better change your RestOfData like this,and use List<RestOfData> to replace OrderedList:

 public class RestOfData
{
            public string id { get; set; }
            public DateTime createdDate { get; set; }
            public string descriptionA { get; set; }
            public string name{ get; set; }
            public DateTime performedDateTime { get; set; }
            public Gender genderA{ get; set; }
            public string codeData{ get; set; }
            public string descriptionB { get; set; }
            public string orderNumber { get; set; }
            public float total{ get; set; }
            public Gender genderB { get; set; }
}

And then use :

List<List1> l = ...;
List<List2> l1 = ...;
List<RestOfData> datalist = (from a in l
                          join b in l1 on a.id equals b.id
                          select new RestOfData()
                          {
                              id=a.id,
                              createdDate=a.createdDate,
                              descriptionA=a.descriptionA,
                              name=a.name,
                              performedDateTime=a.performedDateTime,
                              genderA=a.genderA,
                              codeData=b.codeData,
                              descriptionB=b.descriptionB,
                              orderNumber=b.orderNumber,
                              total=b.total,
                              genderB=b.genderB
                          }).OrderBy(xxx).ToList();
  • Related