Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK!,I try your problem.And I'm great worked.Try my source:</p> <p>My simple Test class:</p> <pre><code>public class Test { [DataMember(Name = "A", Order = 96)] public string A { get; set; } [DataMember(Name = "B", Order = 97)] public string B { get; set; } [DataMember(Name = "C", Order = 98)] public string C { get; set; } } </code></pre> <p>And Ext:</p> <pre><code> public static class Ext { public static IList&lt;KeyValuePair&lt;string, int&gt;&gt; AsOrderColumns&lt;T&gt;(this T t) where T : class { return t.GetType() .GetProperties() .Where(w =&gt; w.IsOrderColumn()) .Select(s =&gt; s.GetOrderColumn()) .OrderBy(o =&gt; o.Value) .ToList(); } private static bool IsOrderColumn(this PropertyInfo prop) { return prop.GetCustomAttributes(typeof(DataMemberAttribute), true) .Any(); } private static KeyValuePair&lt;string, int&gt; GetOrderColumn(this PropertyInfo prop) { var attr = prop.GetCustomAttributes(typeof(DataMemberAttribute), true) .ElementAt(0) as DataMemberAttribute; return (attr != null) ? new KeyValuePair&lt;string, int&gt;(attr.Name, attr.Order) : new KeyValuePair&lt;string, int&gt;(); } public static IList&lt;object&gt; AsOrderRow&lt;T&gt;(this T t) where T : class { return t.GetType() .GetProperties() .Where(w =&gt; w.IsOrderColumn()) .OrderBy(o =&gt; o.GetOrderColumn().Value) .Select(s =&gt; s.GetValue(t, null)) .ToList(); } } </code></pre> <p>Console test code:</p> <pre><code> class Program { static void Main(string[] args) { var test = new Test(); var tests = new List&lt;Test&gt;() { new Test() {A = "A-1.1", B = "B-1.2", C = "C-1.3"}, new Test() {A = "A-2.1", B = "B-2.2", C = "C-2.3"}, new Test() {A = "A-3.1", B = "B-3.2", C = "C-3.3"}, new Test() {A = "A-4.1", B = "B-4.2", C = "C-4.3"} }; Console.WriteLine(String.Join&lt;string&gt;("\t", test.AsOrderColumns().Select(s =&gt; String.Format("{0}({1})", s.Key, s.Value)))); foreach (var item in tests) { Console.WriteLine(String.Join&lt;object&gt;("\t", item.AsOrderRow())); } Console.ReadKey(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload