C# 测试实体中的属性是否可以使用orderBy排序
我正在使用linq动态来执行动态orderBy。如何仅获取必须具有顺序可比类型的实体的属性名称。实体有基本类型(字符串、整数)和复杂对象,我不知道用户根据这些类型进行筛选C# 测试实体中的属性是否可以使用orderBy排序,c#,linq,reflection,C#,Linq,Reflection,我正在使用linq动态来执行动态orderBy。如何仅获取必须具有顺序可比类型的实体的属性名称。实体有基本类型(字符串、整数)和复杂对象,我不知道用户根据这些类型进行筛选 var OrderByOptions = typeof (Project).GetProperties().Select(x => x.Name).ToList(); //I only want order comparable property names here var userSelectable = 2;
var OrderByOptions = typeof (Project).GetProperties().Select(x => x.Name).ToList(); //I only want order comparable property names here
var userSelectable = 2;
var pjs = _pjRepo.Projects.Where(x => x.Active == true).OrderBy(OrderByOptions[userSelectable]).ToList();
我认为所有支持订单可比性的类都应该实现
IComparable
及其通用版本:
var OrderByOptions = typeof (Project).GetProperties()
.Where(p=>typeof(IComparable).IsAssignableFrom(p.PropertyType))
.Select(x => x.Name).ToList();
我认为所有支持订单可比性的类都应该实现
IComparable
及其通用版本:
var OrderByOptions = typeof (Project).GetProperties()
.Where(p=>typeof(IComparable).IsAssignableFrom(p.PropertyType))
.Select(x => x.Name).ToList();
如果要同时处理
IComparable
和IComparable
两者都返回false。但在SQL中,可为null的int肯定是可比的
也许白名单会更好
public static readonly HashSet<Type> ComparableTypes = new HashSet<Type>
{
typeof(bool), typeof(bool?),
typeof(char), typeof(char?),
typeof(string),
typeof(sbyte), typeof(sbyte?), typeof(byte), typeof(byte?),
typeof(short), typeof(short?), typeof(ushort), typeof(ushort?),
typeof(int), typeof(int?), typeof(uint), typeof(uint?),
typeof(long), typeof(long?), typeof(ulong), typeof(ulong?),
typeof(float), typeof(float?),
typeof(double), typeof(double?),
typeof(decimal), typeof(decimal?),
typeof(DateTime), typeof(DateTime?),
typeof(DateTimeOffset), typeof(DateTimeOffset?),
typeof(TimeSpan), typeof(TimeSpan?),
typeof(Guid), typeof(Guid?),
};
var OrderByOptions = (from p in typeof(Project).GetProperties()
let type = p.PropertyType
where ComparableTypes.Contains(type)
select p.Name).ToArray();
public static readonly HashSet compariabletypes=新HashSet
{
类型化(bool),类型化(bool?),
typeof(char),typeof(char?),
类型(字符串),
类型化(sbyte),类型化(sbyte?),类型化(byte),类型化(byte?),
typeof(short),typeof(short?),typeof(ushort),typeof(ushort,
类型of(int),类型of(int?),类型of(uint),类型of(uint,
typeof(long),typeof(long?),typeof(ulong),typeof(ulong?),
类型(浮动),类型(浮动?),
类型(双),类型(双?),
typeof(十进制),typeof(十进制?),
typeof(DateTime),typeof(DateTime?),
typeof(DateTimeOffset),typeof(DateTimeOffset?),
typeof(TimeSpan),typeof(TimeSpan?),
typeof(Guid),typeof(Guid?),
};
var OrderByOptions=(来自typeof(Project.GetProperties()中的p)
let type=p.PropertyType
其中ComparableTypes.Contains(类型)
选择p.Name).ToArray();
如果您想同时处理IComparable
和IComparable
两者都返回false。但在SQL中,可为null的int肯定是可比的
也许白名单会更好
public static readonly HashSet<Type> ComparableTypes = new HashSet<Type>
{
typeof(bool), typeof(bool?),
typeof(char), typeof(char?),
typeof(string),
typeof(sbyte), typeof(sbyte?), typeof(byte), typeof(byte?),
typeof(short), typeof(short?), typeof(ushort), typeof(ushort?),
typeof(int), typeof(int?), typeof(uint), typeof(uint?),
typeof(long), typeof(long?), typeof(ulong), typeof(ulong?),
typeof(float), typeof(float?),
typeof(double), typeof(double?),
typeof(decimal), typeof(decimal?),
typeof(DateTime), typeof(DateTime?),
typeof(DateTimeOffset), typeof(DateTimeOffset?),
typeof(TimeSpan), typeof(TimeSpan?),
typeof(Guid), typeof(Guid?),
};
var OrderByOptions = (from p in typeof(Project).GetProperties()
let type = p.PropertyType
where ComparableTypes.Contains(type)
select p.Name).ToArray();
public static readonly HashSet compariabletypes=新HashSet
{
类型化(bool),类型化(bool?),
typeof(char),typeof(char?),
类型(字符串),
类型化(sbyte),类型化(sbyte?),类型化(byte),类型化(byte?),
typeof(short),typeof(short?),typeof(ushort),typeof(ushort,
类型of(int),类型of(int?),类型of(uint),类型of(uint,
typeof(long),typeof(long?),typeof(ulong),typeof(ulong?),
类型(浮动),类型(浮动?),
类型(双),类型(双?),
typeof(十进制),typeof(十进制?),
typeof(DateTime),typeof(DateTime?),
typeof(DateTimeOffset),typeof(DateTimeOffset?),
typeof(TimeSpan),typeof(TimeSpan?),
typeof(Guid),typeof(Guid?),
};
var OrderByOptions=(来自typeof(Project.GetProperties()中的p)
let type=p.PropertyType
其中ComparableTypes.Contains(类型)
选择p.Name).ToArray();
Notp.GetType()
,p.PropertyType
Notp.GetType()
,p.PropertyType
你应该交换type
和typeof(IComparable)
的位置,我用同样的方法,但我意识到typeof(IComparable)
会先交换。IsAssignableFrom
方法有时让我感到困惑。一个小测试typeof(int).IsAssignableFrom(typeof(IComparable))=false
?但是typeof(IComparable)。IsAssignableFrom(typeof(int))=true
你应该交换type
和typeof(IComparable)
的位置,我也这样做了,但我意识到typeof(IComparable)
将首先执行。IsAssignableFrom
方法有时让我感到困惑。一个小测试typeof(int).IsAssignableFrom(typeof(IComparable))=false
?但是typeof(IComparable).IsAssignableFrom(typeof(int))=true