how do I join two lists using linq or lambda expressions

后端 未结 3 1905
北荒
北荒 2020-12-07 16:41

I have two lists List and List I would like join the two lists on the workorder number as detailed below. In ot

相关标签:
3条回答
  • 2020-12-07 16:52
     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();
    
    0 讨论(0)
  • 2020-12-07 16:54

    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
                };
    
    0 讨论(0)
  • 2020-12-07 17:16

    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}
    );
    
    0 讨论(0)
提交回复
热议问题