Pass an anonymous type into a method

visualbasic
visualbasic used Ask the Experts™
on
I want to pass an anonymous type into a constructor of a class . what is the signature of a anonymous type.

    var VarDraughtItems = from o in NC
                                          where o.DispencedQuantity == 0 && o.SalesQuantity == 0
                                          select new { o.DraughtName, o.SalesQuantity, o.DispencedQuantity };

                    foreach (var value in VarDraughtItems)
                    {
                     
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Anonymous type has no know type at design time. The compiler, JIT, generates a type just before running.
Retired
Distinguished Expert 2017
Commented:
You can create a new class lets say called DraughtItem and then you could return a List<DraughtItem> types from the below query.

Fernando
List<DraughtItem> VarDraughtItems = 
    (from o in NC
     where o.DispencedQuantity == 0 && o.SalesQuantity == 0
     select new DraughtItem { DraughtName = o.DraughtName, 
                              SalesQuantity = o.SalesQuantity, 
                              DispencedQuantity = o.DispencedQuantity }).ToList();

foreach (DraughtItem value in VarDraughtItems)
{

}

======================

public class DraughtItem
{
    public string DraughtName { get; set; }
    public int SalesQuantity { get; set; }
    public int DispencedQuantity { get; set; }
}

Open in new window

Author

Commented:
how do I pass value into mymethod

   foreach (var value in VarDraughtItems)
                    {
                             MyMethod(value);
                    }

Author

Commented:
Thanks for your help!
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Anonymous type have no real value outside of the method it was created in. In your MyMethod subroutine you would have to use reflection to get the values and data types.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial