당신은 linq을 통해이를 달성 할 수 있습니다. 당신이해야 할 일은 당신이 정렬하고자하는 문자열/속성을 전달한 다음 orderby
을 사용하는 것입니다.
일반 예 :
// Create 10 objects with 2 properties
var num_and_name_list =
from num in Enumerable.Range(1,10)
select new { val=num, name=(""+num+"oeu")};
// Here i'll sort them by the name property.
var sorted_by_name_list =
from some_object in num_and_name_list
orderby some_object.name descending
select some_object;
결과는 그래서
처럼, 당신은 단지와 정렬하는 방법/당신이 당신의 재산을 통과 위치를 확인해야합니다 볼 것이다. 당신은 문자열의 속성을 가져해야하는 경우
, 당신은 같은 것을 할 수있는 :
// Create 10 objects with 3 properties
var num_and_name_list =
from num in Enumerable.Range(1, 10)
select new ExtraStringDataPoint (num, num*2, ("" + num + "oeu"));
// Hunting your property starts
Type myType = num_and_name_list.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
PropertyInfo order_on_this = null;
foreach (PropertyInfo prop in props)
{
order_on_this = typeof(ExtraStringDataPoint).GetProperty("Y");
}
// Here i'll sort them by the name property.
var sorted_by_name_list =
from some_object in num_and_name_list
orderby order_on_this descending
select some_object;
내 ExtraStringDataPoint
보이는 같은 : 귀하의 경우
public class ExtraStringDataPoint : IDataPoint
{
public ExtraStringDataPoint(double x, double y, string s)
{
X = x;
Y = y;
Extra = s;
}
public double X { get; set; }
public double Y { get; set; }
public string Extra { get; set; }
public override string ToString()
{
return X +" , " + Y + " , " + Extra;
}
}
, 당신은 할 수 "Y"를 사용하는 것처럼 원하는 변수를 문자열로 전달하십시오.
네, 가능하면 문자열에서 속성을 찾으려면 리플렉션을 사용해야 할 것입니다.하지만 값 비싼 메모리가 현명 할 수 있습니다. 비교 기능을 작성하거나 사용자가 정렬하려는 항목을 찾는 것이 가장 좋은 이유는 무엇입니까? – DGibbs
네, 가능합니다. – James
List이 UI에 바인딩되는 이유는 무엇입니까? UI 컨트롤에서 사용할 수있는 것뿐만 아니라 DataGridView 등 –
Ric