Home > front end >  LINQ Query to solve a complex joining and sorting between two lists to create a new list
LINQ Query to solve a complex joining and sorting between two lists to create a new list

Time:11-24

Looking for a linq query solve this problem. Here are two lists : PartOrders and ServiceOrders there is orderNumber for example 1001X and createdDate on same day for each partOrders and might be many items having same orderNumber so i would sort this list to get only one orderNumber , createdDate and then a list of type . There is also a another list of type ServiceOrders needs also to be sorted the same way as PartOrders list. Finally check if the orderNumber match in booth lists(PartOrders and ServiceOrders) and join them in one orderNumber to get new list of Type .


    public class List<PartOrders>
          {
              public string orderNumber{ get; set; }
              public DateTime createdDate { get; set; }
              public string orderDescription { get; set; }
              public string OderNote { get; set; }
          }
  
  public class List<ServiceOrders>
          {
              public string orderNumber{ get; set; }
              public DateTime createdDate { get; set; }
              public string ServiceDescription { get; set; }
              public string ServiceNote { get; set; }
          }

So the new list would be with type


public class List<Result>
            {
                public string orderNumber{ get; set; }
                public DateTime createdDate { get; set; }
                public List<TheRestOfOrdersData> theRestOfOrdersData  { get; set; }
                public List<TheRestOfServiceData> theRestOfServiceData{ get; set; }
                
            }

i hope this example helps to understand my question.

// PartOrder
List<PartOrders> partOrder = new List<PartOrders>();
// Add item to the list.
partOrder.Add(new Part() { orderNumber = "1001", createdDate = DateTime.Today, orderDescription ="Order spare part", OderNote = "urgent" });
partOrder.Add(new Part() { orderNumber = "1002", createdDate = DateTime.Today, orderDescription = "Not Available", OderNote ="Low"});
partOrder.Add(new Part() { orderNumber = "1002", createdDate = DateTime.Today, orderDescription = "Available", OderNote = "urgent" });
partOrder.Add(new Part() { orderNumber = "1003", createdDate = DateTime.Today, orderDescription ="text", OderNote ="High"});

/// ServiceOrders 
List<ServiceOrders> serviceOrders = new List<ServiceOrders>();
ServiceOrders.Add(new Part() { orderNumber = "1000", createdDate = DateTime.Today, ServiceDescription = "repair", ServiceNote = "medium" });
ServiceOrders.Add(new Part() { orderNumber = "1002", createdDate = DateTime.Today, ServiceDescription = "delivery", ServiceNote = "Low" });
ServiceOrders.Add(new Part() { orderNumber = "1003", createdDate = DateTime.Today, ServiceDescription = "Not Available", ServiceNote = "medium" });
ServiceOrders.Add(new Part() { orderNumber = "1003", createdDate = DateTime.Today, ServiceDescription = "delivery", ServiceNote = "medium" });

the list of type List<Result> will contain the data :
// first item of type Result
{
    orderNumber: "1001",
    createdDate: "Date", 
    List<TheRestOfOrdersData> { orderDescription = "Order spare part", OderNote = "urgent" },
    List<TheRestOfServiceData>{ }
}
// Second
{
orderNumber: "1002",
    createdDate: "Date", 
    List<TheRestOfOrdersData> { orderDescription = "Not Available", OderNote = "Low" },{ orderDescription = " Available", OderNote = "urgent" }
    List<TheRestOfServiceData> { ServiceDescription = "delivery", ServiceNote = "Low" }
}
// and so on
.
.
.
.

CodePudding user response:

 var Result = serviceOrders.Select(x => new Result() { createdDate = x.createdDate, orderNumber = x.orderNumber }).ToList();
 Result.AddRange(partOrder.Select(x => new Result() { createdDate = x.createdDate, orderNumber = x.orderNumber }).ToList());

This is one possible solution, if u want it, this makes it two steps, but allows u to merge the two list into 1 new big list

CodePudding user response:

If there is at least one Order for every service and vice versa. You can get your information with a simple group and join.


// Group order and service on orderNumber;
var groupedOrder = partOrder.GroupBy(x => x.orderNumber);
var groupedService = serviceOrders.GroupBy(x => x.orderNumber);

// Join on the newly created key orderNumber
var joinData = (from o in groupedOrder
    join s in groupedService on o.Key equals s.Key
    select new
    {
        orderNumber = o.Key,
        createdDate = o.First().createdDate,
        orders = o,
        services = s,
    });

Demo : https://dotnetfiddle.net/YlWwL3

  • Related