Home > Mobile >  Sorting and grouping two different lists to one by date and id
Sorting and grouping two different lists to one by date and id

Time:11-22

i had stuck on how to sort two different list into general list and i would if someTne could help me with this, Thank you in advance.

first list for example:

  public class List1
        {
            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 class List2
        {
            public string id{ get; set; }
            public DateTime createdDate { 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; }
        }
public class Gender 
        {
            public string type{ get; set; }
            public string iconName{ get; set; }
        }

my question is

my question is how to make one list with type 

 

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

 public class OrderedList
        {
            public string id { get; set; }
            public DateTime createdDate { get; set; }
            public List<RestOfData> restOfDataa { get; set; }
        }

 public class RestOfData
{
            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; }
}

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