How can I build a LINQ key selector delegate from a property name?
本问题已经有最佳答案,请猛点这里访问。
通常,当提供用户指定的排序顺序,并使用LINQ进行排序时,我会遇到这样一个丑陋的场景:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static IEnumerable<ConfirmationItemViewModel> SortAscending(IEnumerable<ConfirmationItemViewModel> confirmations, string sortProperty) { switch (sortProperty) { case"CreatedOn": confirmations = confirmations.OrderBy(i => i.CreatedOn).ToList(); break; case"PaymentId": confirmations = confirmations.OrderBy(i => i.PaymentId).ToList(); break; default: confirmations = confirmations.OrderBy(i => i.PaymentId).ThenBy(i => i.CreatedOn).ToList(); break; } return confirmations; } |
希望我对它进行编码的尝试(这不起作用)能解释得更多。鉴于我对表达式和委托的理解有限,这是我所能得到的最接近的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static Func<TObject, TKey> BuildKeySelector<TObject, TKey>(TObject source, string propertyName) { return obj => { var prop = source.GetType().GetProperty(propertyName, typeof(TKey)); return (TKey) prop.GetValue(obj); }; } static void Main(string[] args) { // Sort a list of Person objects by their Name property. var peeps = new List<Person>(); var rank = peeps.OrderBy(BuildKeySelector(<something>,"Name")); } |
您不需要
你可以这样做:
1 2 3 4 5 6 7 8 |
但是,这不是很有效,因为您的函数(从