简体   繁体   English

检查实体是否具有相同的属性

[英]Check if the entities have same properties

I'm in a bit of a pickle and I shall describe it here. 我有点发酵,我会在这里描述一下。 I have a foreach loop in which I insert rows in my table. 我有一个foreach循环,我在表中插入行。 But before inserting, I want to check if I already have a row with the same ID, and if so, if the other properties (columns) are of the same values. 但是在插入之前,我想检查我是否已经有一个具有相同ID的行,如果是,那么其他属性(列)是否具有相同的值。

This is what I do: 这就是我做的:

var sourceList = LoadFromOtherDataBase();
var res = ListAll(); // Load all rows from database which were already inserted...


foreach (Whatever w in sourceList)
{
   Entry entry = new Entry();

   entry.id = w.id;
   entry.field1 = w.firstfield;
   entry.field2 = w.secondfield;
   //so on...

   //Now, check if this entry was already inserted into the table...

   var check = res.Where(n => n.id == entry.id);

   if (check.Count() > 0)
   {
     var alreadyInserted = res.Single(n => n.id == entry.id);
     //Here, I need to see if 'alreadyInserted' has the same properties as 'entry' and act upon it. If same properties, do nothing, otherwise, update alreadyInserted with entry's values.
   }
   else
   {
      //Then I'll insert it as a new row obviously....
   }


}

I have thought of Object.Equals() but Entity Framework creates a non-null EntityKey property for alreadyInserted , which is set to null in entry . 我想到了Object.Equals(),但实体框架为alreadyInserted创建了一个非null的EntityKey属性,在入口中设置为null。 Which I think is why it's not working. 我认为这是为什么它不起作用。 EntityKey cannot be set to null. EntityKey不能设置为null。

Any ideas on how to do this without having to check all properties (25+ actually in my case) by hand ? 关于如何做到这一点的任何想法,而不必手动检查所有属性(实际上是我的情况下25+)?

You can use reflection like this: 您可以使用这样的反射:

/// <summary>
/// Check that properties are equal for two instances
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="other"></param>
/// <param name="skipPropeties">A list of names for properties not to check for equality</param>
/// <returns></returns>
public static bool PropertiesEquals<T>(this T first, T other, string[] skipPropeties=null) where T : class
{
    var type = typeof (T);
    if(skipPropeties==null)
        skipPropeties=new string[0];
    if(skipPropeties.Except(type.GetProperties().Select(x=>x.Name)).Any())
        throw new ArgumentException("Type does not contain property to skip");
    var propertyInfos = type.GetProperties()
                                 .Except(type.GetProperties().Where(x=> skipPropeties.Contains( x.Name)));
    foreach (PropertyInfo propertyInfo in propertyInfos)
    {
        if (!Equals(propertyInfo.GetValue(first, null), 
                    propertyInfo.GetValue(other, null)))
            return false;
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查两个ISet是否具有相同实体的正确方法是什么 - What is the correct way to check if two ISets have the same entities 如何检查一个对象的多个属性具有相同的条件? - How to check multiple properties on an object have the same condition? 一个实体内部是否可以具有具有相同基类(TPH)的实体的多个集合属性? - Is it possible to have multiple collection properties of entities with the same base class (TPH) inside one entity? 验证具有相似属性的Entity Framework 6实体 - Validating Entity Framework 6 entities that have similar properties 具有相同属性存储库模式的多个实体 - Multiple Entities with same properties repository pattern 检测具有相同子项的实体 - Detect entities which have the same children 如何在具有两个属性的对象列表中检查具有相同属性的所有对象是否也具有相同的其他属性? - How to check if in a list of Objects with two properties, all Objects with one equal property also have the same other property? INotifyPropertyChanged与具有相同名称的属性 - INotifyPropertyChanged with Properties that have the same name Automapper 8 没有 map 两个具有相同属性但没有配置的实体 - Automapper 8 does not map two entities with same properties without config 反射 - 检查所有可空属性是否具有值 - Reflection - check all nullable properties have values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM