2015-11-03 101 views
0

我有員工列表,其中每個員工包含屬性EmpId,EmpName,PathSuffix.PathSuffix列包含值EmpPart1.lst,EmplPart2.lst,Emp3456.lst,EmpPart3 .lst,Emp5667.lst ....從列表中刪除重複項從<T> C#.net基於屬性值

所以,現在我的問題是如何消除基於pathsuffix列員工的重複,使我的最終列表只包含EmpPart1.lst,Emp3456.lst,Emp5667。第一....(即刪除除一個以外的所有部分文件)。

如何實現這一目標?

輸入: List,每個文件包含的文件路徑的文件名& FilePath.Some與第一部分,第2部分,Part3..whereas結束一些不包含任何parts.My要求是隻需要一個零件文件即

**FileName  FilePath** 
Test1.doc  SourcePart1.lst 
Test2.doc  SourcePart2.lst 
Test3.doc  SourcePart3.lst 
Test4.doc  Event.lst 
Test5.doc  CallPart1.lst 
Test6.doc  CallPart2.lst 
**Desired Output:** 
**FileName  FilePath** 
Test3.doc  SourcePart3.lst 
Test4.doc  Event.lst 
Test6.doc  CallPart2.lst 
+0

請編輯您的問題以包含您的實際類定義(無論它是多長時間)和樣本輸入(作爲可編譯代碼)以及樣本輸出。這使我們很容易給你很好的答案。 – Enigmativity

回答

0

用於解決這方面的一個一般邏輯流將第一

higheset酮(SourcePart3在下面的情況下)選擇第一個僱員,通過列表中刪除任何其它他的路徑後綴保存到變量中,依次循環員工與t他有相同的路徑後綴值。然後增加到您的下一位員工,然後重複。

此外,將員工插入列表時通常更容易檢查重複項,但我沒有您的代碼我不確定您是如何執行此操作的。

2

我們首先創建我們自己的IEqualityComparer<Employee>來比較我們的實體並確定它們是否重複。

public class EmployeeComparer : IEqualityComparer<Employee> 
{ 
    public bool Equals(Employee x, Employee y) 
    { 
     return String.Equals(x.PathSuffix, y.PathSuffix); 
    } 

    public int GetHashCode(Employee obj) 
    { 
     return obj.PathSuffix.GetHashCode(); 
    } 
} 

假如你有如下列表:

List<Employee> employees; 

這是那麼容易,因爲調用IEnumerable(T).Distinct(T)

List<Employee> uniqueEmployees = employees.Distinct(new EmployeeComparer()).ToList(); 
0

我將創建一個Dictionary<string, Employee>。遍歷列表一次,檢查使用的containsKey字典,如果返回true,繼續前進,如果沒有,請致電duct.Add(employee.PathSuffix, employee);

然後使用dict.Values.ToList()

0

我會重新考慮你是如何存儲你的數據。我會把路徑數據放入List中。

Employee對象:Employee對象的

public class Employee 
{ 
    public int EmpId { get; set; } 
    public string EmpName { get; set; } 

    public List<string> PathSuffix { get; set; } 

    public Employee(int ID, string Name, string PathCSV) 
    { 
     EmpId = ID; 
     EmpName = Name; 
     PathSuffix = new List<string>(); 

     foreach (string path in PathCSV.Split(',')) 
     { 
      if (string.IsNullOrEmpty(path) == false && path.Trim().Length > 0) 
      { 
       if (PathSuffix.Exists(x => x.ToLower() == path.Trim().ToLower()) == false) 
       { 
        PathSuffix.Add(path.Trim()); 
       } 
      } 
     } 
    } 
} 

樣品

protected void Page_Load(object sender, EventArgs e) 
    { 
     List<Employee> MyEmployees = new List<Employee>(); 

     MyEmployees.Add(new Employee(1, "User 1", "Path1,Path1,Path2,Path2,Path2,Path3")); 

     Response.Write("Employee 1 Information:<br />"); 
     WriteEmployeeToScreen(MyEmployees[0]); 
    } 

    protected void WriteEmployeeToScreen(Employee ThisEmployee) 
    { 
     System.Text.StringBuilder paths = new System.Text.StringBuilder(); 
     Response.Write(string.Format(
      "{1} ({0})<br />", 
      ThisEmployee.EmpId, 
      HttpUtility.HtmlEncode(ThisEmployee.EmpName))); 


     foreach (string path in ThisEmployee.PathSuffix) 
     { 
      paths.Append(string.Format(", {0}", path)); 
     } 
     if (paths.Length > 0) 
     { 
      Response.Write(string.Format("Paths: {0}", paths.ToString().Substring(2))); 
     } 
    } 

OUTPUT:

Employee 1 Information: 
 
User 1 (1) 
 
Paths: Path1, Path2, Path3