2015-07-28 99 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