如何合并2列表< T>在C#删除重复值 [英] how to merge 2 List<T> with removing duplicate values in C#

查看:162
本文介绍了如何合并2列表< T>在C#删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表名单,我需要相结合,两个列表删除重复值

一个有点难解释,所以让我给什么样的code看起来像一个例子,我想要什么结果,样品我用int型的不ResultAnalysisFileSql类。

first_list = [1,12,12,5]

second_list = [12,5,7,9日,1]

两个列表合并的结果应导致在此列表中:
resulting_list = [1,12,5,7,9]

您会注意到结果具有第一个列表,包括它的两个12的价值观,并在second_list有一个额外的12,1和5的值。

ResultAnalysisFileSql类

  [Serializable接口]
    公共部分类ResultAnalysisFileSql
    {
        公共字符串FileSql {搞定;组; }        公共字符串PathFileSql {搞定;组; }        公开名单< ErrorAnalysisSql>错误{搞定;组; }        公开名单< WarningAnalysisSql>警告{搞定;组; }        公共ResultAnalysisFileSql()
        {        }        公共ResultAnalysisFileSql(字符串fileSql)
        {
            如果(string.IsNullOrEmpty(fileSql)
                || fileSql.Trim()。长度== 0)
            {
                抛出新的ArgumentNullException(fileSql,fileSql为空);
            }            如果(!fileSql.EndsWith(Utility.ExtensionFicherosErrorYWarning))
            {
                抛出新ArgumentOutOfRangeException(fileSql,鲁塔德fichero SQL没有对tiene扩展名+ Uti​​lity.ExtensionFicherosErrorYWarning);
            }            PathFileSql = fileSql;
            FileSql = ObtenerNombreFicheroSql(fileSql);
            错误=新的List< ErrorAnalysisSql>();
            警告=新的List< WarningAnalysisSql>();
        }        私人字符串ObtenerNombreFicheroSql(字符串fileSql)
        {
            变种F = Path.GetFileName(fileSql);
            返回f.Substring(0,f.IndexOf(Utility.ExtensionFicherosErrorYWarning));
        }
        公众覆盖布尔等于(obj对象)
        {
            如果(OBJ == NULL)
                返回false;
            如果(!(obj是ResultAnalysisFileSql))
                返回false;            VAR T = OBJ为ResultAnalysisFileSql;
            返回t.FileSql == this.FileSql
                &功放;&安培; t.PathFileSql == this.PathFileSql
                &功放;&安培; t.Errors.Count == this.Errors.Count
                &功放;&安培; t.Warnings.Count == this.Warnings.Count;
        }
    }

任何样品code的结合并删除重复?


解决方案

你吃过看看可枚举.Union


  

此方法与设置返回排除重复。这是不同
  行为给的毗连
  方法,它返回所有元素
  在包括输入序列
  重复。


 列表< INT> list1的=新的List< INT> {1,12,12,5};
清单< INT>列表2 =新的List< INT> {12,5,7,9日,1};
清单< INT> ulist = list1.Union(列表2).ToList();

I have two lists List that i need to combine and removing duplicate values of both lists

A bit hard to explain, so let me show an example of what the code looks like, and what i want as a result, in sample I use int type not ResultAnalysisFileSql class.

first_list = [1, 12, 12, 5]

second_list = [12, 5, 7, 9, 1]

The result of combining the two lists should result in this list: resulting_list = [1, 12, 5, 7, 9]

You'll notice that the result has the first list, including its two "12" values, and in second_list has an additional 12, 1 and 5 value.

ResultAnalysisFileSql class

[Serializable]
    public partial class ResultAnalysisFileSql
    {
        public string FileSql { get; set; }

        public string PathFileSql { get; set; }

        public List<ErrorAnalysisSql> Errors { get; set; }

        public List<WarningAnalysisSql> Warnings{ get; set; }

        public ResultAnalysisFileSql()
        {

        }

        public ResultAnalysisFileSql(string fileSql)
        {
            if (string.IsNullOrEmpty(fileSql)
                || fileSql.Trim().Length == 0)
            {
                throw new ArgumentNullException("fileSql", "fileSql is null");
            }

            if (!fileSql.EndsWith(Utility.ExtensionFicherosErrorYWarning))
            {
                throw new ArgumentOutOfRangeException("fileSql", "Ruta de fichero Sql no tiene extensión " + Utility.ExtensionFicherosErrorYWarning);
            }

            PathFileSql = fileSql;
            FileSql = ObtenerNombreFicheroSql(fileSql);
            Errors = new List<ErrorAnalysisSql>();
            Warnings= new List<WarningAnalysisSql>();
        }

        private string ObtenerNombreFicheroSql(string fileSql)
        {
            var f = Path.GetFileName(fileSql);
            return f.Substring(0, f.IndexOf(Utility.ExtensionFicherosErrorYWarning));
        }


        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (!(obj is ResultAnalysisFileSql))
                return false;

            var t = obj as ResultAnalysisFileSql;
            return t.FileSql== this.FileSql
                && t.PathFileSql == this.PathFileSql
                && t.Errors.Count == this.Errors.Count
                && t.Warnings.Count == this.Warnings.Count;
        }


    }

Any sample code for combine and removing duplicates ?

解决方案

Have you had a look at Enumerable.Union

This method excludes duplicates from the return set. This is different behavior to the Concat method, which returns all the elements in the input sequences including duplicates.

List<int> list1 = new List<int> { 1, 12, 12, 5};
List<int> list2 = new List<int> { 12, 5, 7, 9, 1 };
List<int> ulist = list1.Union(list2).ToList();

这篇关于如何合并2列表&LT; T&GT;在C#删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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