我要如何在C#中的两个列表的区别? [英] How do i get the difference in two lists in C#?

查看:124
本文介绍了我要如何在C#中的两个列表的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有两个列出了C#

 列表<属性与GT;属性=新的List<属性与GT;();
清单<串GT;歌曲=新的List<串GT;();

一个是串和一个是说我created..very简单的一个属性对象的

 类属性
{
    公共字符串大小{搞定;组; }
    公共字符串链接{搞定;组; }
    公共字符串名称{;组; }
    公共属性(){}
    公共属性(字符串s,串升,串N)
    {
        大小=秒;
        链接= 1;
        名称= N;
    }
}

我现在来比较,看看有什么歌曲是不是在属性名称,以便例如:

  songs.Add(东西);
songs.Add(其他);
songs.Add(另一个);属性=新的属性(500,http://google.com,东西);
attributes.Add(一);

我希望有一个方法,返回另一个和另一个,因为他们不是在属性列表名称

所以伪code

 差值=歌曲 -  attributes.names


解决方案

 变量差值= songs.Except(attributes.Select(S = GT; s.name))。了ToList() ;

修改

新增了ToList(),使之成为列表

Ok so I have two lists in C#

List<Attribute> attributes = new List<Attribute>();
List<string> songs = new List<string>();

one is of strings and and one is of a attribute object that i created..very simple

class Attribute
{
    public string size { get; set; }
    public string link { get; set; }
    public string name { get; set; }
    public Attribute(){}
    public Attribute(string s, string l, string n) 
    {
        size = s;
        link = l;
        name = n;
    }
}

I now have to compare to see what songs are not in the attributes name so for example

songs.Add("something"); 
songs.Add("another"); 
songs.Add("yet another");

Attribute a = new Attribute("500", "http://google.com", "something" ); 
attributes.Add(a);

I want a way to return "another" and "yet another" because they are not in the attributes list name

so for pseudocode

difference = songs - attributes.names

解决方案

var difference = songs.Except(attributes.Select(s=>s.name)).ToList();

edit

Added ToList() to make it a list

这篇关于我要如何在C#中的两个列表的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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