我有两个列表List<WorkOrder>
和List<PlannedWork>
我想加入工作订单号上的两个列表,详情如下。换句话说,我有一份计划工作清单,但我需要知道workOrderNumber的工作描述。
我是linq和lambda表达式的新手,我还没有把头缠在他们身上。
class WorkOrder
{
public int WorkOrderNumber { get; set; }
public string WorkDescription { get; set; }
}
class PlannedWork
{
public int WorkOrderNumber { get; set; }
public DateTime ScheduledDate { get; set; }
}
答案 0 :(得分:72)
听起来你想要这样的东西:
var query = from order in workOrders
join plan in plans
on order.WorkOrderNumber equals plan.WorkOrderNumber
select new
{
order.WorkOrderNumber,
order.Description,
plan.ScheduledDate
};
答案 1 :(得分:72)
使用扩展方法而不是linq查询语法的方法是这样的:
var results = workOrders.Join(plans,
wo => wo.WorkOrderNumber,
p => p.WorkOrderNumber,
(order,plan) => new {order.WorkOrderNumber, order.WorkDescription, plan.ScheduledDate}
);
答案 2 :(得分:2)
public class State
{
public int SID { get; set; }
public string SName { get; set; }
public string SCode { get; set; }
public string SAbbrevation { get; set; }
}
public class Country
{
public int CID { get; set; }
public string CName { get; set; }
public string CAbbrevation { get; set; }
}
List<State> states = new List<State>()
{
new State{ SID=1,SName="Telangana",SCode="+91",SAbbrevation="TG"},
new State{ SID=2,SName="Texas",SCode="512",SAbbrevation="TS"},
};
List<Country> coutries = new List<Country>()
{
new Country{CID=1,CName="India",CAbbrevation="IND"},
new Country{CID=2,CName="US of America",CAbbrevation="USA"},
};
var res = coutries.Join(states, a => a.CID, b => b.SID, (a, b) => new {a.CName,b.SName}).ToList();