簡體   English   中英

比較自定義列表中的對象並返回不匹配的對象C#

[英]Compare object in custom list and return the unmatched object C#

我有兩個模型

public class NewrecordModel
{
    public string NewName { get; set; }
    public string NewFileName { get; set; }
    public string NewFileVersion { get; set; }
}

public class OldrecordModel
{
    public string OldName { get; set; }
    public string OldFileName { get; set; }
    public string OldFileVersion { get; set; }
}

我需要比較的屬性, NewNameOldNameNewFileVersionOldFileVersion和報酬的差別NewrecordModel在列表中。

我嘗試了以下一種

var unMatchedRecord = NewrecordModel.Where(o => !OldrecordModel.Any(n => n.OldName == o.NewName) || !OldrecordModel.Any(n => n.OldFileVersion == o.NewFileVersion)); 

上面的一個在列表中返回不匹配的NewrecordModel ,並且工作正常,但是我需要比較OldFileVersion小於NewFileVersion並立即返回列表。 在查詢中使用OldFileVersion小於NewFileVersion ,它會將不相關的數據列為輸出。

以下是我嘗試比較的查詢,

var unMatchedVersion = NewrecordModel.Where(o => OldrecordModel.Any(n => n.OldFileVersion.ToInt() < o.NewFileVersion.ToInt()));

以上是linq是正確的。 如何比較linq中的數字並返回結果。

我為獲取屬性名稱和值編寫了此內容,然后進行了更改。 希望對您有所幫助。

public static class CompareObject
    {
        /// <summary>
        /// Compares the properties of two objects of the same type and returns if all properties are equal.
        /// </summary>
        /// <param name="objectA">The first object to compare.</param>
        /// <param name="objectB">The second object to compre.</param>
        /// <param name="ignoreList">A list of property names to ignore from the comparison.</param>
        /// <returns><c>true</c> if all property values are equal, otherwise <c>false</c>.</returns>
        public static Dictionary<string,object> AreObjectsEqual(object objectA, object objectB, params string[] ignoreList)
        {
            //bool result;
            var list = new Dictionary<string, object>();
            if (objectA != null && objectB != null)
            {
                Type objectType;

                objectType = objectA.GetType();

                //result = true; // assume by default they are equal

                foreach (PropertyInfo propertyInfo in objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead && !ignoreList.Contains(p.Name)))
                {


                    // if it is a primative type, value type or implements IComparable, just directly try and compare the value
                    if (CanDirectlyCompare(propertyInfo.PropertyType))
                    {
                        object valueA;
                        object valueB;

                        valueA = propertyInfo.GetValue(objectA, null);
                        valueB = propertyInfo.GetValue(objectB, null);
                        if (!AreValuesEqual(valueA, valueB))
                        {
                            list.Add(propertyInfo.Name, valueA);
                            //Console.WriteLine("Mismatch with property '{0}.{1}' found.", objectType.FullName, propertyInfo.Name);
                            //result = false;
                        }
                    }
                }
            }
            //else
            //    result = object.Equals(objectA, objectB);

            return list;
        }

        /// <summary>
        /// Determines whether value instances of the specified type can be directly compared.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        ///     <c>true</c> if this value instances of the specified type can be directly compared; otherwise, <c>false</c>.
        /// </returns>
        private static bool CanDirectlyCompare(Type type)
        {
            return typeof(IComparable).IsAssignableFrom(type) || type.IsPrimitive || type.IsValueType;
        }

        /// <summary>
        /// Compares two values and returns if they are the same.
        /// </summary>
        /// <param name="valueA">The first value to compare.</param>
        /// <param name="valueB">The second value to compare.</param>
        /// <returns><c>true</c> if both values match, otherwise <c>false</c>.</returns>
        private static bool AreValuesEqual(object valueA, object valueB)
        {
            bool result;
            IComparable selfValueComparer;

            selfValueComparer = valueA as IComparable;

            if (valueA == null && valueB != null || valueA != null && valueB == null)
                result = false; // one of the values is null
            else if (selfValueComparer != null && selfValueComparer.CompareTo(valueB) != 0)
                result = false; // the comparison using IComparable failed
            else if (!object.Equals(valueA, valueB))
                result = false; // the comparison using Equals failed
            else
                result = true; // match

            return result;
        }

    }

並使用

var changedColumns = CompareObject.AreObjectsEqual(NewrecordModel, OldrecordModel);

它可能是NewrecordModel集合中的一個對象,為此OldrecordModel集合中沒有相同名稱的對象,或者OldrecordModel集合中任何名稱相同的對象都有一個較小的OldFileVersion

檢查以下代碼:

    var unMatchedVersion = 
        newrecordModel.Where(o => !oldrecordModel.Any(n => n.OldName == o.NewName) ||
        oldrecordModel.Any(n => n.OldName == o.NewName && 
        int.Parse(n.OldFileVersion) < int.Parse(o.NewFileVersion)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM