2012-09-25 31 views
0

我有一個類似於此的數據庫表。LINQ狀態必須返回截然不同

ID | 狀態 | 類型

等...等

我使用LINQ,試圖從這個集合辨別不同以下狀態,像這樣

results = ctx.Status.Distinct(new StatusComparer()).ToList(); 

但這返回所有的狀態,我使用以下Link構建下面的比較器,我發現在另一個StackOverflow中的建議Post

public class StatusComparer : IEqualityComparer<Status> 
{ 
    public bool Equals(Status x, Status y) 
    { 
     // Check whether the compared objects reference the same data. 
     if (ReferenceEquals(x, y)) 
     { 
      return true; 
     } 

     // Check whether any of the compared objects is null. 
     if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) 
     { 
      return false; 
     } 

     // Check whether the status' properties are equal. 
     return x.StatusDescription == y.StatusDescription && x.Type == y.Type && x.StatusID == y.StatusID; 
    } 

    public int GetHashCode(Status status) 
    { 
     // Get hash code for the Name field if it is not null. 
     var hashStatusId = status.StatusID.GetHashCode(); 

     // Get hash code for the Code field. 
     var hashStatusDescription = status.StatusDescription.GetHashCode(); 

     var hashStatusType = status.Type.GetHashCode(); 

     // Calculate the hash code for the product. 
     return hashStatusId^hashStatusDescription^hashStatusType; 
    } 
} 
} 

我的問題如下,我們有一個系統工作得很好,實際上他們想要另一個系統使用相同的數據庫,所以我們探測它。搜索有幾個過濾器的高級選項之一他們是狀態,但你可以從上面看到(鬆散的)數據庫結構狀態有不同的類型,但類似的文本。我需要能夠通過獨特的文本通過Linq選擇整個狀態。所有的幫助將不勝感激。

也試過

results = (from s in context.Status group s by s.StatusDescription into g select  g.First()).ToList(); 

這也未能與System.NotSupportedException

+1

我重新標記這是'C#'你。這絕對不是'c'。 (); –

+0

謝謝你減速更快;) – Deviland

+0

已嘗試結果=(從s中的context.Status組s通過s.StatusDescription into g select g.First())。這也失敗了 – Deviland

回答

2

要選擇所有不同的狀態:

ctx.Status.Select(s => new { s.StatusDescription, s.Type }).Distinct(); 
+1

我認爲需要組合StatusDescription和StatusType來確定這個值。 –

+0

當我嘗試這樣做時,我沒有將Status作爲intellisense的一個選項,但我試過了,但這並不能編譯,你能解釋一下你試圖向我展示什麼嗎? – Deviland

+0

我認爲你是對的。編輯。 –