2013-10-03 202 views
0

這是我的linq查詢,我得到了很多重複的學校名稱。 所以我創建了一個正則表達式功能修剪文字:從linq查詢c刪除重複項#

public static string MyTrimmings(string str) 
     { 
     return Regex.Replace(str, @"^\s*$\n", string.Empty, RegexOptions.Multiline).TrimEnd(); 
     } 

文本被trimed正常的,但是,在下拉值都是重複的!請幫我消除重複,哦Linq快樂!

ViewBag.schools = new[]{new SelectListItem 
      { 
       Value = "", 
       Text = "All" 
      }}.Concat(
      db.Schools.Where(x => (x.name != null)).OrderBy(o => o.name).ToList().Select(s => new SelectListItem 
      { 
       Value = MyTrimmings(s.name), 
       Text = MyTrimmings(s.name) 
      }).Distinct() 
      );  
+0

我不知道不同的取值很好的瞭解。謝謝我現在會嘗試。 – NULL

+0

它說不能lambda表達式比較的IEqualityComparer錯誤 – NULL

+1

參見[morelinq的DistinctBy(https://code.google.com/p/morelinq/source/browse/MoreLinq/DistinctBy.cs?r=d4396b9ff63932be0ab07c36452a481d20f96307) –

回答

0

假設你有一個School類,你可以寫一個IEqualityComparer

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

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

     //Check whether the school' properties are equal. 
     return x.Name == y.Name; 
    } 

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(School school) 
    { 
     //Check whether the object is null 
     if (Object.ReferenceEquals(school, null)) return 0; 

     //Get hash code for the Name field if it is not null. 
     int hashSchoolName = school.Name == null ? 0 : school.Name.GetHashCode(); 

     //Calculate the hash code for the school. 
     return hashSchoolName; 
    } 
} 

那麼你的LINQ查詢應該是這樣的:

db.Schools.Where(x => x.name != null) 
      .OrderBy(o => o.name).ToList() 
      .Distinct(new SchoolComparer()) 
      .Select(s => new SelectListItem 
      { 
       Value = MyTrimmings(s.name), 
       Text = MyTrimmings(s.name) 
      }); 
+0

我得到他的錯誤:LINQ to Entities無法識別方法'System.Linq.IQueryable'1 [namespace.Models.School] Distinct [School](System.Linq.IQueryable'1 [namespace.Models.School],System .Collections.Generic.IEqualityComparer'1 [namespace.Models.School])'方法,並且此方法不能轉換爲商店表達式。 – NULL

+0

我更新了答案,我把Distinct放在了錯誤的地方 –

+0

@EstebanElverdin引入整個結果集並在客戶端執行工作,在數據庫端添加了很多成本,它們都可以更快速地(通常)並減少網絡流量。另外,即使你想要查詢運行一個LINQ to objects方法,你也應該使用AsEnumerable而不是ToList來避免急於執行查詢,迫使整個查詢在內存中而不是讓它存在流,並不必要地創建,然後丟棄一個List對象。 – Servy

4

Distinct差,GroupBy爲贏:

db.Schools.GroupBy(school => school.name).Select(grp => grp.First()); 
0

您可以讓您的班級實施IEquatable<T>界面,因此Distinct將知道如何比較它們。像這樣的(基本的例子):

public class SelectListItem : IEquatable<SelectListItem> 
{ 
    public string Value { get; set; } 
    public string Text { get; set; } 

    public bool Equals(SelectListItem other) 
    { 
     if (other == null) 
     { 
      return false; 
     } 

     return Value == other.Value && Text == other.Text; 
    } 

    public override int GetHashCode() 
    { 
     unchecked 
     { 
      int hash = 17; 

      if (Value != null) 
      { 
       hash = hash * 23 + Value.GetHashCode(); 
      } 

      if (Text != null) 
      { 
       hash = hash * 23 + Text.GetHashCode(); 
      } 

      return hash; 
     } 
    } 
} 

(採取約翰·弗朗飛碟雙向的答案在這裏的GetHashCode:https://stackoverflow.com/a/263416/249000