如何比较 2 List<string>从 List<string> 中获取缺失值的对象 [英] How to compare 2 List&lt;string&gt; objects to get the missing value from the List&lt;string&gt;

查看:16
本文介绍了如何比较 2 List<string>从 List<string> 中获取缺失值的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用NOT IN"获取缺失的数据,添加到foo"列表中.

How do I use the "NOT IN" to get the missing data, to be added to "foo" List.

var accessories = new List<string>(); 
var foo = new List<string>();

accessories.Add("Engine");
accessories.Add("Tranny");
accessories.Add("Drivetrain");
accessories.Add("Power Window");

foo.Add("Engine");
foo.Add("Tranny");
foo.Add("Power Window");

foreach(var v in foo.Where(x => x??).???)
{
    foo.Add(v);  //Add the missing "Drivetrain" to it...
}

推荐答案

使用 列表.除:

foo.AddRange(accessories.Except(foo));

来自 MSDN:

Except 产生两个序列的差集.

Except Produces the set difference of two sequences.

这篇关于如何比较 2 List<string>从 List<string> 中获取缺失值的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆