简体   繁体   English

如何比较两个列表并更新列表值

[英]How to compare two lists and update a list value

Hi i am trying the below requirement.嗨,我正在尝试以下要求。

class: class:

public class Items
    {
        public string ID { get; set; }
        public string NAME { get; set; }
        public string type { get; set; }


    }

list1:清单1:

        ID NAME type
        1  aaa  1a
        2  bbb  2b
        3  ccc  3b
    

list2:清单2:

        ID NAME 
        1a  aaa  
        2b  bbb  
        3c  ccc 
        4d  ddd
        5e  eee
        6f  fff

Compare the two lists list1 and list2.比较两个列表 list1 和 list2。

i am trying the below query Compare the two lists list1 and list2.我正在尝试以下查询比较两个列表 list1 和 list2。

List<Items> list3= list2.Where(x => list1.Any(z=>x.id==z.type)).ToList();

i am getting the below result for above query:对于上述查询,我得到以下结果:

        ID NAME 
        1a  aaa  
        2b  bbb  
        3c  ccc 

expected result: i want the list1 ID, list2 Name where list1.type==list2.ID to form the list3 like below预期结果:我想要 list1 ID,list2 Name where list1.type==list2.ID 形成如下的 list3

list3:
    
        ID NAME 
        1  aaa  
        2  bbb  
        3  ccc 

Any Help or Suggestion to the above query.对上述查询的任何帮助或建议。 Thanks, Sudha.谢谢,苏达。

Join two lists on values of id and type fields and then select desired result:加入两个关于idtype字段值的列表,然后加入 select 所需的结果:

from l1 in list1
join l2 in list2 on l1.type equals l2.id
select new { l1.id, l1.name }

In the select operator you can select whole l1 , or create non-anonymous type instance.在 select 运算符中,您可以 select 整个l1 ,或创建非匿名类型实例。 Same in method syntax:方法语法相同:

list1.Join(list2, l1 => l1.type, l2 => l2.id, (l1, l2) => new { l1.id, l1.name })

Note: use PascalCase naming for properties.注意:对属性使用 PascalCase 命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM