mardi 30 août 2016

How to set property name using reflection in anonymous list?

So I have this query:

using (APContext _context = new APContext(null)) {
                var q = from inv in _context.Invoices
                        where inv.Sys_InvoiceID == 1276291
                        join cust in _context.InvoiceCustomFields
                        on inv.Sys_InvoiceID equals cust.FK_SysInvoiceID

                        select new {
                                inv.Sys_InvoiceID,
                                inv.AccountNumber,
                                cust.FK_CustomFieldHeader,
                                cust.Value
                        };
            }

Which returns something like so { 1, 50, "Badger", "Is Great"}

I would like to use the value of FK_customFieldHeader to set a property name in an anonymous list like so:

      var x = q.Select(r => new{
                    ID = r.Sys_InvoiceID,
                    AccountNum = r.AccountNumber,
                    //Reflection here to set CustomFieldHeader value as the property name and cust.Value to be the value for the property
                    r.FK_CustomFieldHeader = r.Value //Like this
        });

How do I do this?





Aucun commentaire:

Enregistrer un commentaire