2015-07-28 58 views
0

我有兩個相同的集合,其中1個傳遞給視圖,另一個集合存儲在會話中,然後顯示視圖。將兩個集合與linq和populate變量進行比較

我們在視圖上使用隱藏字段,這些隱藏字段內部是SenderId。當用戶提交頁面時,我想將提交的SenderId與Session集合中的SenderId進行比較,並填入一個變量與Id不匹配(這將意味着用戶已經篡改了隱藏字段)

This是我目前有:

var storedValues = (List<MailBox>)Session["Mail"]; 
var noMatch = (from x in model where storedValues.Any(s => s.SenderId != x.SenderId) select x.SenderId).ToList(); 

但無論我做什麼它總是選擇所有的人,即使我改變SenderId對視圖和調試時,我可以看到我已經篡改了SenderId,我可以對於我的生活來說,讓變量noMatch來填充我篡改的不正確的SenderId。

任何幫助,將不勝感激。

更新模型聲明:

public class MailBox 
{ 
    public Int64 SenderId { get; set; } 

    public Int64 RecipientId { get; set; } 

    public string Username { get; set; } 

    public int TotalMessages { get; set; } 

    public string PhotoId { get; set; } 

    public bool NewMessages { get; set; } 

    public DateTime LastLoggedIn { get; set; } 

    public DateTime LatestEmailDate { get; set; } 

    public bool LoggedIn { get; set; } 

    public string Message { get; set; } 

    public bool Delete { get; set; } 
} 
+0

我想嘗試另一種語法,如:storedValues.Where (x => x.SenderId!= s.SenderId) –

+0

@SebastianL我試過了,但我得到的錯誤:參數不能分配給參數類型bool –

+1

請說明'model'是如何定義的。 – Kapol

回答

0

如果集合是相同的大小,你可以使用.ZIP()

collection1.Zip(colection2, (col1_item, col2_item) => 
{ 
    if(col1_item.SomeProperty == col2_item.SomeProperty) 
    { 
     // 
    } 
}); 
0

我已經成功,現在解決這個問題,通過執行以下操作:

var noMatch = model.Where(item => item.Delete && storedValues.Any(x => x.SenderId == item.SenderId)).Select(item => item.SenderId).ToList(); 
0

使用以下GenericListComparer,爲T型,這是MailBox在你的情況下,你需要重寫GetHashCodeEquals方法。這樣,您可以在兩個列表中的任何一箇中返回不同的元素集合,並且可以執行許多其他操作。

internal class GenericListComparer<T> 
    { 
internal static IEnumerable<T> FindOnlySecondListElements(IEnumerable<T> firstCollection, 
      IEnumerable<T> secondCollection) 
     { 
      // Create Hashset from first list 
      HashSet<int> firstCollectionHashSet = 
       new HashSet<int>(firstCollection.Select(element => element.GetHashCode())); 

      // Fetch elements only in second type list 
      List<T> onlySecondListElements = 
       secondCollection.Where(element => !firstCollectionHashSet.Contains(element.GetHashCode())).ToList(); 

      return onlySecondListElements; 
     } 
} 

代碼重寫EqualsGetHashCode和等於在MailBox

public class MailBox : IEquatable<MailBox> 
    { 
     public Int64 SenderId { get; set; }  

     public bool Equals(MailBox other) 
     { 
      //Check whether the compared object is null. 
      if (Object.ReferenceEquals(other, null)) return false; 

      //Check whether the compared object references the same data. 
      if (Object.ReferenceEquals(this, other)) return true; 

      //Check whether the CustomEntity properties are equal. 
      return SenderId.Equals(other.SenderId); 
     } 

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

    public override int GetHashCode() 
    { 
     // Get hash code for the Id field 
     return SenderId.GetHashCode(); 
     } 

    } 

多個領域,也可以oevrriden檢查以下link