Exemplo n.º 1
0
        public static ListComparisonResult <T> CompareLists <T>(IList newList, IList existingList, List <string> propertyNames, Type returnType)
        {
            ListComparisonResult <T> result = new ListComparisonResult <T>();

            if (existingList == null || existingList.Count == 0)
            {
                result.NewItems = (IList)GetListOfGivenType(newList, returnType);
            }
            else
            {
                result.NewItems = GetItemsNotPresentInSource <T>(existingList, newList, propertyNames, returnType, ref result);
            }

            if (newList == null || newList.Count == 0)
            {
                result.DeletedItems = (IList)GetListOfGivenType(existingList, returnType);
            }
            else
            {
                ListComparisonResult <T> nullObj = null;
                result.DeletedItems = GetItemsNotPresentInSource <T>(newList, existingList, propertyNames, returnType, ref nullObj);
            }

            if (result.CommonItems.Count == 0)
            {
                result.CommonItems = (IList)Activator.CreateInstance(returnType);
            }

            return(result);
        }
Exemplo n.º 2
0
        private static IList GetItemsNotPresentInSource <T>(IList listToCompare, IList source, List <string> propertyNames, Type returnType, ref ListComparisonResult <T> result)
        {
            object returnObj        = Activator.CreateInstance(returnType);
            object commonListObject = Activator.CreateInstance(returnType);

            if (source != null)
            {
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (var item in source)
                {
                    List <object> sourceProperties = new List <object>();

                    //add all comparison properties to a list
                    foreach (string propertyName in propertyNames)
                    {
                        PropertyInfo propertyInfo = properties.FirstOrDefault(x => x.Name == propertyName);
                        if (propertyInfo != null)
                        {
                            object sourceValue = propertyInfo.GetValue(item, null);
                            sourceProperties.Add(sourceValue);
                        }
                    }

                    if (listToCompare != null)
                    {
                        // compare with each element in the other list
                        if (!ContainsSameItem(sourceProperties, listToCompare, propertyNames))
                        {
                            ((IList)returnObj).Add((T)item);
                        }
                        else
                        {
                            if (result != null)
                            {
                                ((IList)commonListObject).Add((T)item);
                            }
                        }
                    }
                }
            }

            if (result != null)
            {
                result.CommonItems = (IList)commonListObject;
            }

            return((IList)returnObj);
        }