Results 1 to 7 of 7

Thread: [RESOLVED] List of Anonymous objects

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,142

    Resolved [RESOLVED] List of Anonymous objects

    This whole question is because I'm lazy, but that's the driver of all kinds of things, so that's fine.

    I am getting back a datatable with an annoying number of columns. It's not utterly untenable, but it is annoying. I'm turning that into a list of classes to bind to a specific third-party control. The code to do this is pretty compact and unimpressive. The one annoying thing is that I need an actual class with a property for each of the columns returned from the datatable. The set of columns is static, so I could write out such a class fairly quickly with just a bit of tedious work. However, this is for a report on the web. The code is pretty small and tidy, and I have a bit more work to do to get the output to look and behave the way I want, but that's straightforward.

    The issue is that once I have this small and tidy project running, I'll then have to turn around and create up to a dozen more, each of which will get a different datatable with a different, large, set of columns. Creating all these very similar projects will be a snap, with the one exception being the creation of that tedious darn class for use in the list. Heck, that class is the ONLY place I have to enter all the field names. I can even make them all strings, though I probably shouldn't. Furthermore, the sole way that this class will be used is as a holder of a datarow in a List<T>.

    I don't care how many fields there are and I don't care what the class is called. I only care about the field names, which become the property names, maybe the type (I haven't really decided, yet, though a goodly number of the fields are strings already), and that's it.

    That sounds like an anonymous type, or at least a semi-anonymous type. It's probably asking too much, but is it possible to create a list<anonymous>?

    It probably won't work anyways, because I'd then have to expose that list via a bindable property, which would mean a property of anonymous type, and that seems a step too far.
    My usual boring signature: Nothing

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,544

    Re: List of Anonymous objects

    Quote Originally Posted by Shaggy Hiker View Post
    This whole question is because I'm lazy, but that's the driver of all kinds of things, so that's fine.

    I am getting back a datatable with an annoying number of columns. It's not utterly untenable, but it is annoying. I'm turning that into a list of classes to bind to a specific third-party control. The code to do this is pretty compact and unimpressive. The one annoying thing is that I need an actual class with a property for each of the columns returned from the datatable. The set of columns is static, so I could write out such a class fairly quickly with just a bit of tedious work. However, this is for a report on the web. The code is pretty small and tidy, and I have a bit more work to do to get the output to look and behave the way I want, but that's straightforward.

    The issue is that once I have this small and tidy project running, I'll then have to turn around and create up to a dozen more, each of which will get a different datatable with a different, large, set of columns. Creating all these very similar projects will be a snap, with the one exception being the creation of that tedious darn class for use in the list. Heck, that class is the ONLY place I have to enter all the field names. I can even make them all strings, though I probably shouldn't. Furthermore, the sole way that this class will be used is as a holder of a datarow in a List<T>.

    I don't care how many fields there are and I don't care what the class is called. I only care about the field names, which become the property names, maybe the type (I haven't really decided, yet, though a goodly number of the fields are strings already), and that's it.

    That sounds like an anonymous type, or at least a semi-anonymous type. It's probably asking too much, but is it possible to create a list<anonymous>?

    It probably won't work anyways, because I'd then have to expose that list via a bindable property, which would mean a property of anonymous type, and that seems a step too far.
    You can't return a list on anonymous types, but you can return a list of Tuples e.g.

    Code:
    static void Main(string[] args)
    {
        var results = GetStuff();
        foreach (var result in results)
        {
            Console.WriteLine(result);
            Console.WriteLine(result.num2);
        }
    }
    
    static List<(int num1, int num2, string name)> GetStuff()
    {
        return new List<(int num1, int num2, string name)>
        {
            (1,2,"a"),
            (2,3,"b"),
            (3,4,"c"),
        };
    }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,458

    Re: List of Anonymous objects

    Anonymous types can generally only be used in the same context in which they are created, because only that context knows what properties they have and can thus access those properties at compile time. There are various Razor methods that allow you to pass in an anonymous object, e.g. for HTML attributes on a generated tag, but those methods use Reflection on those objects. This shortcoming doesn't apply if you're not accessing the properties at compile time, e.g. you bind a list of an anonymous type to a WinForms ComboBox or ListBox and you assign the names of the properties as Strings to the DisplayMember and ValueMember.

  4. #4

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,142

    Re: List of Anonymous objects

    Well, that's what I expected. The issue really isn't the list, it's hooking whatever into a third party control. That whatever can be a few different things, and the class was the first one I got working. I had to use reflection to some extent, and upon reflecting on the subject, I'm thinking I should also try a different approach a bit more. After all, the third party control would also accept a JSON array, though I haven't gotten that to work, just yet.

    Still, I figured I'd ask to make sure I wasn't overlooking anything.
    My usual boring signature: Nothing

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,801

    Re: [RESOLVED] List of Anonymous objects

    What version of the .NET framework are you using? The reason I ask is because in the .NET framework you could install an ORM called PetaPoco that came with a t4 template to generate the strongly typed classes for you.

    So long as you're not using complex primary keys or anything else out of the "ordinary" then it worked very well.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,142

    Re: [RESOLVED] List of Anonymous objects

    Not Framework. This is .NET 8.
    My usual boring signature: Nothing

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,801

    Re: [RESOLVED] List of Anonymous objects

    Ah, they deprecated it in .NET core.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width