-
C# List<type> 중복제거프로그래밍/C# 2019. 4. 14. 21:33
List의 Distinct를 쓰면 된다.
단, List에 타입이 string이나 int 같이 속성이 하나일 때만 된다.
속성이 여러개이면 중복제거가 되지 않으므로
중복을 제거할 값의 타입으로 먼저 리스트를 만들고 값을 넣어 중복제거하고 다른 리스트에 담으면 된다.
다음은 카톡 대화내용 분석에서 방문자리스트를 만들 때 사용한 방법이다.
// 방문자 이름만 담을 리스트
List<string> list = new List<string>();
for (int i = 3; i < text.Length; i++)
{
if (text[i].Length > 0)
{
if (text[i].Substring(0, 1) == "[")
{
string[] s = text[i].Split(' ');
try
{
if (s[1].IndexOf("[오") > -1)
{
list.Add(s[0]);
}
}
catch
{
continue;
}
}
}
}
// 중복제거
list = list.Distinct().ToList();
// Person 리스트에 담기
List persons = new List();
foreach (string s in list)
{
Persons ps = new Persons();
ps.iCount = 0;
ps.sName = s;
persons.Add(ps);
}// 방문자는 속성을 여러개 가짐
public class Persons
{
public string sName { get; set; }
public string sInDate { get; set; }
public string sOutDate { get; set; }
public string sFinalDate { get; set; }
public string sFinalLog { get; set; }
public int iCount { get; set; }
}'프로그래밍 > C#' 카테고리의 다른 글
C# 요일 구하기 DayOfWeek 메서드 (0) 2019.04.20 C# How to export data direct from reportviewer to excel. (0) 2019.04.19 C# List<type> 중복제거 (0) 2019.04.14 C# 데이터그리드뷰 CSV 파일로 저장하기 (0) 2019.04.14 C# TextBox의 MultiLine을 사용할 때 라인(줄) 수 구하기 (0) 2019.04.09 C# 숫자만 입력되게 하는 방법들 (0) 2019.04.05