2015-08-09 54 views
-1

我已经创建了一个方法,它将根据属性列表从对象列表创建动态对象列表。在这种情况下,我已经使用Expandoobject完成了这个任务。但是我没有创建这种expando对象列表的清晰列表。请访问以下fidder并查看我的代码。创建独特的expando对象列表

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

+2

我想你应该改一下你的问题。 –

回答

1

代码块中只需添加以下代码。

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