C# 创建不同的expando对象列表
我创建了一个方法,该方法将根据属性列表从对象列表创建动态对象列表。在本例中,我使用Expandoobject完成了这样的任务。但我未能创建此类expando对象列表的独特列表。请访问以下fidder并查看我的代码C# 创建不同的expando对象列表,c#,visual-studio,C#,Visual Studio,我创建了一个方法,该方法将根据属性列表从对象列表创建动态对象列表。在本例中,我使用Expandoobject完成了这样的任务。但我未能创建此类expando对象列表的独特列表。请访问以下fidder并查看我的代码 public class Program { public static void Main() { var _dynamicObjectList = new List<Student>(); for (int i = 0;
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;
}
}
}
公共类程序
{
公共静态void Main()
{
var_dynamicObjectList=新列表();
对于(int i=0;i<5;i++)
{
_dynamicObjectList.Add(新学生{ID=i,Name=“stu”+i,Address=“Address”+i,录取日期=DateTime.Now.AddDays(i),年龄=15岁,父亲姓名=“Jamal”+i,母亲姓名=“Jamila”+i});
}
//再次创建以检查不同的列表
对于(int i=0;i<5;i++)
{
_dynamicObjectList.Add(新学生{ID=i,Name=“stu”+i,Address=“Address”+i,录取日期=DateTime.Now.AddDays(i),年龄=15岁,父亲姓名=“Jamal”+i,母亲姓名=“Jamila”+i});
}
//var returnList=test2.GetDdlData(_dynamicObjectList,“ID,Name,Address”);
//var returnList=test2.GetDdlData(_dynamicObjectList,“ID,父名,地址”);
var returnList=test2.GetDdlData(_dynamicObjectList,“ID,Name,FatherName,MotherName,Age”);
字符串strSerializeData=JsonConvert.SerializeObject(returnList);
Console.WriteLine(strSerializeData);
Console.ReadLine();
}
}
公立班学生
{
公共int?ID{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共日期时间许可日期{get;set;}
公共字符串父名称{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共静态类test2
{
公共静态IList GetDdlData(此IEnumerable源,字符串userParams)
{
尝试
{
List otherProperties=userParams.Split(',').ToList();
Dictionary parentPropertyInfo=新字典();
Dictionary parentType=新字典();
var dynamicObjectList=(从源代码中的k选择k.ToList();
如果(dynamicObjectList.Count()>0)
{
//若parentField存在,则系统将处理父属性
如果(otherProperties.Count>0)
{
foreach(otherProperties中的字符串otherProperty)
{
//获取父字段属性信息
PropertyInfo\u info=dynamicObjectList[0].GetType().GetProperty(otherProperty);
parentPropertyInfo.Add(其他属性,\u信息);
//获取父字段属性类型
Type pType=null.getUnderlineType(_info.PropertyType)??_info.PropertyType;
parentType.Add(otherProperty,pType);
}
}
}
//返回列表
IList objList=新列表();
foreach(源中的目标)
{
var dynamicObj=作为IDictionary的新ExpandooObject();
foreach(otherProperties中的字符串otherProperty)
{
PropertyInfo objPropertyInfo=parentPropertyInfo.FirstOrDefault(m=>m.Key==otherProperty).Value;
类型objPropertyType=parentType.FirstOrDefault(m=>m.Key==otherProperty).Value;
对象数据=(objPropertyInfo.GetValue(obj,null)=null)?null:Convert.ChangeType(objPropertyInfo.GetValue(obj,null),objPropertyType,null);
dynamicObj.Add(其他属性、数据);
}
添加对象列表(dynamicObj);
}
var returnUniqList=objList.Distinct().ToList();
返回优衣库;
}
捕获(例外情况除外)
{
掷骰子;
}
}
}
只需在代码块中添加以下代码
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;
}
}
foreach(objList中的var objTemp){
bool不相似=真;
foreach(otherProperties中的字符串属性){
//获取并发送对象属性数据
对象tempFValue=(对象temp作为IDictionary)[property];
//获取当前对象属性数据
对象tempCValue=(dynamicObj作为IDictionary)[property];
如果(!tempFValue.Equals(tempCValue)){
IsNotsimular=假;
打破
}
}
如果(不相似){
isDuplicate=真;
打破
}
}
我认为你应该重新表述你的问题。