在C#中按另一个对象的属性排序对象列表

时间:2020-12-14 21:32:26

We have a system that is based around Events that Users are invited to [UserInvite].

我们有一个基于事件的系统,用户被邀请到[UserInvite]。

Currently, I have a page that lists all the Users invited to an Event; they can be sorted by various properties, such as Surname [ascending and descending]. All fine and dandy, but now I'm required to additionally sort by a UserInvite property: DateEdited [again asc/desc]. I've been using an extension method that takes in a string of the property name to sort by, but this won't work for another class's property.

目前,我有一个页面列出了邀请参加活动的所有用户;它们可以按各种属性排序,例如姓氏[升序和降序]。所有罚款和花花公子,但现在我需要另外按UserInvite属性排序:DateEdited [再次asc / desc]。我一直在使用一个扩展方法,它接受一个属性名称的字符串来排序,但是这不适用于另一个类的属性。

Notes:

  1. UserInvite has a method to get by Event and User which always returns a single UserInvite [or null if no invite was found].
  2. UserInvite有一个方法来获取Event和User,它总是返回一个UserInvite [如果没有找到邀请则为null]。

  3. The current EventID is held in the Master page so can be reached on the page that lists the Users
  4. 当前的EventID保存在Master页面中,因此可以在列出Users的页面*问

What is the best way to do this?

做这个的最好方式是什么?

1 个解决方案

#1


Is there a route from the Event to the UserInvite? If so, simply walk the relation. Actually, the simplest approach is to re-expose this date on the Event - i.e.

是否有从事件到UserInvite的路线?如果是这样,只需走一段关系。实际上,最简单的方法是在事件上重新公开这个日期 - 即

public DateTime InviteDate {get {return Invite.Date;}} // or similar

and order by InviteDate - but you can also use a string like "Invite.Date" and split it by dots - for example, here's something similar for IQueryable<T> - although it doesn't handle null at the moment.

并且通过InviteDate排序 - 但你也可以使用像“Invite.Date”这样的字符串并用点分割 - 例如,这里有类似于IQueryable 的东西 - 虽然它目前不处理null。

#1


Is there a route from the Event to the UserInvite? If so, simply walk the relation. Actually, the simplest approach is to re-expose this date on the Event - i.e.

是否有从事件到UserInvite的路线?如果是这样,只需走一段关系。实际上,最简单的方法是在事件上重新公开这个日期 - 即

public DateTime InviteDate {get {return Invite.Date;}} // or similar

and order by InviteDate - but you can also use a string like "Invite.Date" and split it by dots - for example, here's something similar for IQueryable<T> - although it doesn't handle null at the moment.

并且通过InviteDate排序 - 但你也可以使用像“Invite.Date”这样的字符串并用点分割 - 例如,这里有类似于IQueryable 的东西 - 虽然它目前不处理null。