2리스트를 병합하는 방법 C #에서 중복 값을 제거합니다.
세 번째 목록으로 결합하고 해당 목록에서 중복 값을 제거 해야하는 두 개의 목록이 있습니다.
조금 설명하기 어려우므로 코드의 모양과 결과로 원하는 결과의 예를 보여 드리겠습니다. 샘플에서는 ResultAnalysisFileSql 클래스가 아닌 int 유형을 사용합니다.
first_list = [1, 12, 12, 5]
second_list = [12, 5, 7, 9, 1]
두 목록을 결합한 결과는 다음 목록이됩니다. 결과 _ 목록 = [1, 12, 5, 7, 9]
결과에는 두 개의 "12"값을 포함하여 첫 번째 목록이 있고 second_list에는 추가 12, 1 및 5 값이 있습니다.
ResultAnalysisFileSql 클래스
[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;
}
}
중복을 결합하고 제거하기위한 샘플 코드는 무엇입니까?
Enumerable을 살펴 보셨습니까?
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();
why not simply eg
var newList = list1.Union(list2)/*.Distinct()*//*.ToList()*/;
oh ... according to msdn you can leave out the .Distinct()
This method excludes duplicates from the return set
Union has not good performance : this article describe about compare them with together
var dict = list2.ToDictionary(p => p.Number);
foreach (var person in list1)
{
dict[person.Number] = person;
}
var merged = dict.Values.ToList();
Lists and LINQ merge: 4820ms
Dictionary merge: 16ms
HashSet and IEqualityComparer: 20ms
LINQ Union and IEqualityComparer: 24ms
Use Linq's Union:
using System.Linq;
var l1 = new List<int>() { 1,2,3,4,5 };
var l2 = new List<int>() { 3,5,6,7,8 };
var l3 = l1.Union(l2).ToList();
List<int> first_list = new List<int>() {
1,
12,
12,
5
};
List<int> second_list = new List<int>() {
12,
5,
7,
9,
1
};
var result = first_list.Union(second_list);
'Programing' 카테고리의 다른 글
jQuery를 사용하여 요소 ID에서 콜론 처리 (0) | 2020.06.19 |
---|---|
전체 디렉토리 트리에 대한 줄 끝 변환 (Git) (0) | 2020.06.19 |
유형 또는 네임 스페이스 이름 'DbContext'를 찾을 수 없습니다. (0) | 2020.06.19 |
문자열에 적어도 하나의 소문자, 대문자, 숫자 및 기호가 포함되어 있는지 확인 (0) | 2020.06.19 |
변수에 이름이 지정된 JavaScript 함수 호출 (0) | 2020.06.19 |