通用列表的动态分类标准 [英] Dynamic Sort Criteria for Generic List
问题描述
这是我目前的代码:
public override List< oAccountSearchResults> SearchForAccounts(oAccountSearchCriteria searchOptions)
{
列表< oAccountSearchResults> results = Service.SearchForAccounts(searchOptions); ((a1,a2)=> a2.AccountNumber.CompareTo(a1.AccountNumber));
results.Sort
返回结果;
}
我想要做的是提供一个参数,告诉我哪个字段排序。然后动态更新我的排序条件,而不用像这样的一堆if()语句:
public override List< oAccountSearchResults> SearchForAccounts(oAccountSearchCriteria searchOptions,string sortCriteria)
{
List< oAccountSearchResults> results = Service.SearchForAccounts(searchOptions);
if(sortCriteria ==AccountNumber)
{
results.Sort((a1,a2)=> a2.AccountNumber.CompareTo(a1.AccountNumber));
else if(sortCriteria ==FirstName)
{
results.Sort((a1,a2)=> a2.FirstName.CompareTo(a1.FirstName) );
}
返回结果;
}
我想这样做不需要大约30个if()语句可用的可排序标准。
任何及所有帮助将不胜感激。
编辑解决方案:
谢谢大家的回应。
大卫,你的接近工作,但我认为理查德的答案有点作用更好。
这是我想出的最终解决方案。我使用David的例子和Richards实现框架:
using System;
使用System.Collections.Generic;
命名空间SortTest
{
class Program
{
static void Main(string [] args)
{
var results1 =搜索(oObject => oObject.Value1);
foreach(在result1中的oObject o)
{
Console.WriteLine(o.Value1 +,+ o.Value2);
}
Console.WriteLine(Environment.NewLine);
var results2 =搜索(oObject => oObject.Value2);
foreach(在result2中的oObject o)
{
Console.WriteLine(o.Value1 +,+ o.Value2);
}
Console.ReadLine();
}
public static List< oObject>搜索< T>(Func< oObject,T> keyExtract)其中T:IComparable
{
var results = new List< oObject>
{
new oObject {Value1 =A 1,Value2 =B 2},
new oObject {Value1 =B 1,Value2 =A 2}
}; ((a,b)=> keyExtract(a).CompareTo(keyExtract(b)));
results.Sort
返回结果;
}
}
class oObject
{
public string Value1 {get;组; }
public string Value2 {get;组; }
}
}
如果调用者可以提供一个提取用于比较的值的表达式,那么可以在比较函数中调用该委托:
public override List< oAccountSearchResults> SearchForAccounts< T>(
oAccountSearchCriteria searchOptions,
Func< oAccountSearchResults,T> keyExtract)其中T:IComparable {
List< oAccountSearchResults> results = Service.SearchForAccounts(searchOptions);
results.Sort(a,b)=> keyExtract的(a).CompareTo(keyExtract(B)));
返回结果;
}
The purpose of this is to avoid writing a ton of if() statements.
Here is my current code:
public override List<oAccountSearchResults> SearchForAccounts(oAccountSearchCriteria searchOptions)
{
List<oAccountSearchResults> results = Service.SearchForAccounts(searchOptions);
results.Sort((a1, a2) => a2.AccountNumber.CompareTo(a1.AccountNumber));
return results;
}
What I would like to do is provide a parameter which tells me which field to sort on. Then dynamically update my sort criteria without having a bunch of if() statements such as this:
public override List<oAccountSearchResults> SearchForAccounts(oAccountSearchCriteria searchOptions, string sortCriteria)
{
List<oAccountSearchResults> results = Service.SearchForAccounts(searchOptions);
if (sortCriteria == "AccountNumber")
{
results.Sort((a1, a2) => a2.AccountNumber.CompareTo(a1.AccountNumber));
}
else if (sortCriteria == "FirstName")
{
results.Sort((a1, a2) => a2.FirstName.CompareTo(a1.FirstName));
}
return results;
}
I would like to do this without having about 30 if() statements for all the sortable criteria that will be available.
Any and all help will be appreciated.
EDIT WITH SOLUTION:
Thank you all for your responses.
David, your approached worked but I think that Richard's answer works a bit better.
Here is the ultimate solution that I came up with. I used David's framework for the example and Richards implementation:
using System;
using System.Collections.Generic;
namespace SortTest
{
class Program
{
static void Main(string[] args)
{
var results1 = Search(oObject => oObject.Value1);
foreach (oObject o in results1)
{
Console.WriteLine(o.Value1 + ", " + o.Value2);
}
Console.WriteLine(Environment.NewLine);
var results2 = Search(oObject => oObject.Value2);
foreach (oObject o in results2)
{
Console.WriteLine(o.Value1 + ", " + o.Value2);
}
Console.ReadLine();
}
public static List<oObject> Search<T>(Func<oObject, T> keyExtract) where T: IComparable
{
var results = new List<oObject>
{
new oObject {Value1 = "A 1", Value2 = "B 2"},
new oObject {Value1 = "B 1", Value2 = "A 2"}
};
results.Sort((a, b) => keyExtract(a).CompareTo(keyExtract(b)));
return results;
}
}
class oObject
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
}
If the caller could supply an expression which extracts the value to use to compare, you can call that delegate in the comparison function:
public override List<oAccountSearchResults> SearchForAccounts<T>(
oAccountSearchCriteria searchOptions,
Func<oAccountSearchResults, T> keyExtract) where T : IComparable {
List<oAccountSearchResults> results = Service.SearchForAccounts(searchOptions);
results.Sort(a,b) => keyExtract(a).CompareTo(keyExtract(b)));
return results;
}
这篇关于通用列表的动态分类标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!