我读了这个topic并申请了我的代码。
//listSort contains ExpandoObjects
List<dynamic> listSort = new List<dynamic>(listDynamic.Count);
foreach (var item in listDynamic)
{
dynamic objDynamic = OrderBySpecialCharacter.ConvertToExpand(item);
string sortValue = OrderBySpecialCharacter.GetValueInExpandoObject(objDynamic, colSortText);// "Name");
objDynamic.Sort = OrderBySpecialCharacter.ConvertToACSII(sortValue.ToUpper());
listSort.Add(objDynamic);
}
List<dynamic> sortedList = new List<dynamic>();
if (colSort2 == null && colSort3 == null)
{
//Error in below line
sortedList = listSort.OrderBy(x=> x.GetReflectedPropertyValue("Sort"),new MrFourCompare<string>()).ToList();
}
----------------
public static ExpandoObject ConvertToExpand<T>(T objInput)
{
ExpandoObject objExpand = new ExpandoObject();
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
IDictionary<String, Object> iExpand = ((IDictionary<String, Object>)objExpand);
foreach (PropertyDescriptor prop in properties)
iExpand.Add(prop.Name, prop.GetValue(objInput));
return (ExpandoObject)iExpand;
}
--------------------
我看到了错误:
严重级代码描述项目文件行抑制状态 错误CS0411方法的类型参数 &#39; Enumerable.OrderBy(IEnumerable,Func,IComparer)&#39; 无法从使用中推断出来。尝试 指定类型参数 显式。 DemoDynamicObject C:\ Users \ MrFour-IT \ Desktop \ DemoDynamicObject \ DemoDynamicObject \ OrderBySpecialCharacter.cs 154 Active
我不知道如何解决它!请帮我!感谢
答案 0 :(得分:0)
如果您有 if test == 1:
OP_1(list_counter, repeat_question)
while repeat_question != True:
repeat_question, t_class_right, repeat_question = Repeat(repeat_question, option_right, t_class_right)
elif test == 2:
OP_2(list_counter, repeat_question)
while repeat_question != True:
repeat_question, t_class_right, repeat_question = Repeat(repeat_question, option_right, t_class_right)
elif test == 3:
OP_3(list_counter, repeat_question)
while repeat_question != True:
repeat_question, t_class_right, repeat_question = Repeat(repeat_question, option_right, t_class_right)
else:
T_Broke()
喜欢此代码:
IComparer
根据您推荐的链接,您最常更改此行:
public class MrFourCompare: IComparer<String>
{
public int Compare(string x, string y)
{
return string.Compare(x, y);
}
}
到
sortedList = listSort.OrderBy(x=> x.GetReflectedPropertyValue("Sort"),new MrFourCompare<string>()).ToList();
因为在sortedList = listSort.OrderBy(x=> x.GetReflectedPropertyValue("Sort"),new MrFourCompare()).ToList();
的实现中,您将其设置为字符串。
修改强>
IComparer
不能是IComparer
,因此如果您想按动态值的排序属性排序,则最常将排序属性转换为精确类型,如Dynamic
或String
。
Int