在我的应用过程中的某个时候,我获得了来自数据库查询的数据字典。我将该数据和实例一个对象调用并调用RestoreState()
方法,该方法将数据字典列入数据,并将值应用于新实例中的相应属性。
RestoreState()
方法完成后,然后将值字典传递给对象的字典将其传递给一个称为OriginalValues
的属性:
myObject.EnableChangeTracking = true;
myObject.OrginalValues = dictionaryData;
稍后,用户通过UI对对象进行了一些更改,并按下保存。然后,我想将对象属性的现有状态与原始状态进行比较,因此我可以构建一个更新语句以发送到SQL。
问题(和问题)是我的lambda表达式将我当前的属性价值与字典中的值进行比较。 OriginalValues
字典有23个项目,我的lambda返回的字典包含14个。在我的单位测试中,我只更改2.为什么我会返回12个尚未更改的额外值?
我的getchanges方法:
public Dictionary<string, object> GetChanges()
{
if(!EnableChangeTracking || OrginalValues==null || !OrginalValues.Any())
return new Dictionary<string, object>();
// Only get properties that have changed.
var changedProperties =
this
.GetType()
.GetProperties()
.Where(p => p.GetValue(this, null) != OrginalValues[p.Name])
.ToDictionary(p => p.Name, p => p.GetValue(this));
return changedProperties;
}
我的OriginalValues
字典包含:
- [0] {[Assetid,1]} System.Collections.generic.keyvaluepair
- [1] {[subfeatureid,0]} system.collections.generic.keyvaluepair
- [2] {[[orgnumber,8555]} system.collections.generic.keyvaluepair
- [3] {[laneLatoceId,]} system.collections.generic.keyvaluepair
- [4] {[statusid,1]} system.collections.generic.keyvaluepair
- [5] {[AssetCode,1]} System.Collections.generic.keyvaluepair
- [6] {[注意,这里没有什么可见的。]} system.collections.generic.keyvaluepair
- [7] {[DateActivated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [8] {[DateInactivated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [9] {[Enteredby,js]} system.collections.generic.keyvaluepair
- [10] {[dateentered,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvalueepair
- [11] {[createby,js]} system.collections.generic.keyvaluepair
- [12] {[更新,JS]} system.collections.generic.keyvaluepair
- [13] {[DateCreated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [14] {[dateupdated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvalueepair
- [15] {[isActive,true]} system.collections.generic.keyvaluepair
- [16] {[[AssetAttributes,system.collections.generic.list`1 [fisdc.models.assetattribute]]} system.collections.generic.keyvaluepair
- [17] {[[assetreason,system.collections.generic.list'1 [fisdc.models.assetreason]]}} system.collections.generic.keyvaluepair
- [18] {[[AssetObjects,system.collections.generic.list`1 [fisdc.models.assetobject]]} System.Collections.generic.keyvaluepair
- [19] {[subfeature,]} system.collections.generic.keyvaluepair
- [20] {[EnableChangetracking,false]} system.collections.generic.keyvaluepair
- [21] {[iSvalid,false]} system.collections.generic.keyvaluepair
- [22] {[[orginalValues,]} system.collections.generic.keyvaluepair
和我的lambda表达式返回:
- [0] {[Assetid,1]} System.Collections.generic.keyvaluepair
- [1] {[subfeatureid,0]} system.collections.generic.keyvaluepair
- [2] {[[orgnumber,2222]} system.collections.generic.keyvaluepair
- [3] {[statusid,1]} system.collections.generic.keyvaluepair
- [4] {[DateActivated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [5] {[DateInactivated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [6] {[dateentered,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [7] {[createby,someyelse]} system.collections.generic.keyvaluepair
- [8] {[DateCreated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [9] {[DateUpdated,1/21/2014 9:12:08 AM]} System.Collections.generic.keyvaluepair
- [10] {[isActive,true]} system.collections.generic.keyvaluepair
- [11] {[enableChangEtracking,true]} system.collections.generic.keyvaluepair
- [12] {[isvalid,false]} system.collections.generic.keyvaluepair
- [13] {[orginalValues,system.collections.generic.dictionary`2 [system.string,system.Object]]} system.collections.generic.generic.keyvaluepair
我在设备测试中更改的唯一属性是Orgnumber和创建。有人有任何想法吗?
update
以下是我用来更新属性的代码,然后调用GetChanges()
方法。
myObject.OrgNumber = "2222";
myObject.CreatedBy = "SomeoneElse";
myDictionary = myObject.GetChanges();
更新2
我很好奇问题是否是我的字典是<string, object>
,并且我正在与参考和非参考类型进行比较?因此,将int值与对象进行比较并返回?我不知道。
更新3
@ttat的工作方法
public Dictionary<string,object> OrginalValues { get; set; }
public Dictionary<string, object> GetChanges()
{
if(!EnableChangeTracking || OrginalValues==null || !OrginalValues.Any())
return new Dictionary<string, object>();
// Only get properties that have changed.
var changedProperties =
this
.GetType()
.GetProperties()
.Where(p => !System.Attribute.IsDefined(p, typeof(IgnoreAttribute))
&& !Equals(p.GetValue(this, null), OrginalValues[p.Name]))
.ToDictionary(p => p.Name, p => p.GetValue(this));
return changedProperties;
}
由于您的字典是<string, object>
,因此!=
将使用参考比较。使用Equals
方法检查值。这使用基本平等,将为您处理无效结果。
Where(p => !Equals(p.GetValue(this, null), OrginalValues[p.Name]))