Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sure; you can do that in many ways; starting with reflection (note, this is slowish - OK for moderate amounts of data though):</p> <pre><code>var props = objectType.GetProperties(); foreach(object obj in data) { foreach(var prop in props) { object value = prop.GetValue(obj, null); // against prop.Name } } </code></pre> <p>However; for larger volumes of data it would be worth making this more efficient; for example here I use the <code>Expression</code> API to pre-compile a delegate that looks writes each property - the advantage here is that no reflection is done on a per-row basis (this should be significantly faster for large volumes of data):</p> <pre><code>static void Main() { var data = new[] { new { Foo = 123, Bar = "abc" }, new { Foo = 456, Bar = "def" }, new { Foo = 789, Bar = "ghi" }, }; string s = Write(data); } static Expression StringBuilderAppend(Expression instance, Expression arg) { var method = typeof(StringBuilder).GetMethod("Append", new Type[] { arg.Type }); return Expression.Call(instance, method, arg); } static string Write&lt;T&gt;(IEnumerable&lt;T&gt; data) { var props = typeof(T).GetProperties(); var sb = Expression.Parameter(typeof(StringBuilder)); var obj = Expression.Parameter(typeof(T)); Expression body = sb; foreach(var prop in props) { body = StringBuilderAppend(body, Expression.Property(obj, prop)); body = StringBuilderAppend(body, Expression.Constant("=")); body = StringBuilderAppend(body, Expression.Constant(prop.Name)); body = StringBuilderAppend(body, Expression.Constant("; ")); } body = Expression.Call(body, "AppendLine", Type.EmptyTypes); var lambda = Expression.Lambda&lt;Func&lt;StringBuilder, T, StringBuilder&gt;&gt;(body, sb, obj); var func = lambda.Compile(); var result = new StringBuilder(); foreach (T row in data) { func(result, row); } return result.ToString(); } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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