0

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"}, { 2, 51, "Cat", "Is Cool"}

So I have two items in a list(shown above) and I need to use both FK_CustomFieldHeaders values as properties in an anonymous list like so:

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

How do I do this?

EDIT: My question is different because I'm not talking about setting one property here. The query returns two. Check the update.

EDIT 2: It doesn't have to be reflection. I'd just like some help and explanation please.

Andrew Kilburn
  • 2,251
  • 6
  • 33
  • 65
  • I don't know the solution, but why do you think **reflection** is the solution? – Maarten Aug 30 '16 at 14:06
  • @Maarten Maybe it's not, but I have seen other answers where the user has used reflection to set a property name to be a value of a variable – Andrew Kilburn Aug 30 '16 at 14:07
  • I misunderstood your goal at first, sorry. There is no difference between anonymous or *normal* type if you are asking about [code-generation](http://stackoverflow.com/q/3862226/1997232). When using anonymous type you don't care about name, simply generate [random one](http://stackoverflow.com/a/1344242/1997232) each time. – Sinatr Aug 30 '16 at 14:08
  • Probably you can't. Check this out : http://stackoverflow.com/questions/6044482/setting-anonymous-type-property-name – jambonick Aug 30 '16 at 14:22

0 Answers0