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

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

问题描述

我有两个列表我需要组合和删除两个列表的重复值的列表



有点难以解释,所以让我举个例子代码看起来像我想要的结果,在样例我使用int类型不是ResultAnalysisFileSql类。



first_list = [1,12,12,5] p>

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



组合这两个列表的结果应该是在此列表中:
due_list = [1,12,5,7,9]



您会注意到结果有第一个列表,其中包括两个12值,而在second_list中还有一个额外的12,1和5值。



ResultAnalysisFileSql类

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

public string PathFileSql {get;组; }

public List< ErrorAnalysisSql>错误{get;组; }

public List< WarningAnalysisSql>警告{get;组; }

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 tieneextensión + Utility.ExtensionFicherosErrorYWarning);
}

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

私有字符串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是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;
}


}

任何示例代码为了合并和删除重复项目?

解决方案

你有看过 Enumerable.Union


此方法从返回集合中排除重复项。这对于Concat
方法是不同的
行为,它返回输入序列中所有元素
,包括
重复。




 列表< int> list1 = new List< int> {1,12,12,5}; 
列表< int> list2 = new List< int> {12,5,7,9,1};
列表< int> ulist = list1.Union(list2).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 List&lt; T&gt;删除C#中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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