Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I join two lists using linq or lambda expressions

Tags:

c#

I have two lists List<WorkOrder> and List<PlannedWork> I would like join the two lists on the workorder number as detailed below. In other words I have a list of planned work but I need to know the description of the work for the workOrderNumber.

I am new to both linq and lambda expressions, and I haven't quite got my head wrapped around them yet.

class WorkOrder {     public int WorkOrderNumber { get; set; }     public string WorkDescription { get; set; } }  class PlannedWork {     public int WorkOrderNumber { get; set; }     public DateTime ScheduledDate { get; set; } } 
like image 924
Brad Avatar asked Jun 06 '11 14:06

Brad


2 Answers

The way to do this using the Extention Methods, instead of the linq query syntax would be like this:

var results = workOrders.Join(plans,   wo => wo.WorkOrderNumber,   p => p.WorkOrderNumber,   (order,plan) => new {order.WorkOrderNumber, order.WorkDescription, plan.ScheduledDate} ); 
like image 170
NerdFury Avatar answered Sep 22 '22 23:09

NerdFury


It sounds like you want something like:

var query = from order in workOrders             join plan in plans                  on order.WorkOrderNumber equals plan.WorkOrderNumber             select new             {                 order.WorkOrderNumber,                 order.Description,                 plan.ScheduledDate             }; 
like image 20
Jon Skeet Avatar answered Sep 24 '22 23:09

Jon Skeet