创建不同的扩展对象列表

本文关键字:对象 列表 扩展 创建 | 更新日期: 2023-09-27 18:06:58

我创建了一个方法,它将根据属性列表从对象列表创建动态对象列表。在本例中,我使用Expandoobject完成了这样的任务。但是我没有创建这样的扩展对象列表的不同列表。请访问下面的fidder并查看我的代码。

public class Program
{
    public static void Main()
    {
     var _dynamicObjectList = new List<Student>();
            for (int i = 0; i < 5; i++)
            {
                _dynamicObjectList.Add(new Student { ID = i, Name = "stu" + i, Address = "address" + i, AdmissionDate = DateTime.Now.AddDays(i) , Age=15, FatherName="Jamal"+i, MotherName = "Jamila"+i});
            }
            //create again for checking distinct list
            for (int i = 0; i < 5; i++)
            {
                _dynamicObjectList.Add(new Student { ID = i, Name = "stu" + i, Address = "address" + i, AdmissionDate = DateTime.Now.AddDays(i), Age = 15, FatherName = "Jamal" + i, MotherName = "Jamila" + i });
            }

         //   var returnList = test2.GetDdlData<Object>(_dynamicObjectList, "ID,Name,Address");
         //   var returnList = test2.GetDdlData<Object>(_dynamicObjectList, "ID,FatherName,Address");
            var returnList = test2.GetDdlData<Object>(_dynamicObjectList, "ID,Name,FatherName,MotherName,Age");
            string strSerializeData = JsonConvert.SerializeObject(returnList);
            Console.WriteLine(strSerializeData);
            Console.ReadLine();
    }   
}
  public class Student
    {
        public int? ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public DateTime AdmissionDate { get; set; }
        public string FatherName { get; set; }
        public string MotherName { get; set; }
        public int Age { get; set; }
    }
   public static class test2
    {
        public static IList GetDdlData<T>(this IEnumerable<T> source, string userParams)
        {
            try
            {
                List<string> otherProperties = userParams.Split(',').ToList();
                Dictionary<string, PropertyInfo> parentPropertyInfo = new Dictionary<string, PropertyInfo>();
                Dictionary<string, Type> parentType = new Dictionary<string, Type>();
                var dynamicObjectList = (from k in source select k).ToList();
                if (dynamicObjectList.Count() > 0)
                {
                    //if parentField exists then system will handle parent property 
                    if (otherProperties.Count > 0)
                    {
                        foreach (string otherProperty in otherProperties)
                        {
                            //get parent field  property info
                            PropertyInfo _info = dynamicObjectList[0].GetType().GetProperty(otherProperty);
                            parentPropertyInfo.Add(otherProperty, _info);
                            //get parent field  propertyType
                            Type pType = Nullable.GetUnderlyingType(_info.PropertyType) ?? _info.PropertyType;
                            parentType.Add(otherProperty, pType);
                        }
                    }
                }
                //return List 
                IList<object> objList = new List<object>();
                foreach (T obj in source)
                {
                    var dynamicObj = new ExpandoObject() as IDictionary<string, Object>;
                    foreach (string otherProperty in otherProperties)
                    {
                        PropertyInfo objPropertyInfo = parentPropertyInfo.FirstOrDefault(m => m.Key == otherProperty).Value;
                        Type objPropertyType = parentType.FirstOrDefault(m => m.Key == otherProperty).Value;
                        Object data = (objPropertyInfo.GetValue(obj, null) == null) ? null : Convert.ChangeType(objPropertyInfo.GetValue(obj, null), objPropertyType, null);
                        dynamicObj.Add(otherProperty, data);
                    }
                    objList.Add(dynamicObj);
                }

                var returnUniqList = objList.Distinct().ToList();
                return returnUniqList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
https://dotnetfiddle.net/hCuJwD

创建不同的扩展对象列表

只需在代码块中添加以下代码:

foreach(var objTemp in objList) {
    bool isNotSimilar = true;
    foreach(string property in otherProperties) {
        //get sending object property data
        object tempFValue = (objTemp as IDictionary < string, Object > )[property];
        //get current  object property data
        object tempCValue = (dynamicObj as IDictionary < string, Object > )[property];
        if (!tempFValue.Equals(tempCValue)) {
            isNotSimilar = false;
            break;
        }
    }
    if (isNotSimilar) {
        isDuplicate = true;
        break;
    }
}

DOTNETFIDDLE