Anonymous type collections?

前端 未结 7 1261
醉梦人生
醉梦人生 2021-01-05 02:47

I am looking for best practices for creating collections made from anonymous types.

There are several approaches - this one and most answers on this thread assume th

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 03:13

    I stumbled upon this question while looking for a way to create a temporary list of temporary objects. Why would I want to create such a thing? I was looking for a quick way to return JSON for a list of objects made from some values of another object. Here's what I mean.

    Suppose I have an Employee class on the server side which looks like the following in C#:

    public class Employee
    {
              public int Id;
           public int businessGroupId;
           public string title;
           public int positionId;
    
           public string firstName;
           public string lastName;
           ...
    }
    

    Furthermore I have a collection of those objects which I can easily iterate through, something like the following:

    List allEmployees  = new List();
    

    Now, I want to return to my calling web page the entire collection of Employees, but I only want the firstName, lastName for each. I want them returned as JSON.

    Here's The Important Point

    I do not want to return every property in the Employee Object.

    I know I can easily create an anonymous type by iterating through the allEmployees collection and building a new object each time.

    It would look something like the following:

    foreach (Employee e in allEmployees)
    {
        var tempObj = new {F_Name=e.firstName, L_Name=e.lastName};
    }
    

    What's The Type Of An Anonymous Type? :)

    The trick now is that I want an entire collection (List<>)of these anonymous objects. However, I have no way to provide the List a type that I will be using. I cannot create a List allTempObjects = new List();

    Or can I?

    With dynamic, It Is Possible

    I can do the following and it works great:

    List allEmployees  = new List();
    
    foreach (Employee e in allEmployees)
    {
               var tempObj = new {F_Name=e.firstName, L_Name=e.lastName};
            allEmployees.Add(tempObj);
    }
    
    // use the JavaScript Serializer to serialize the dynamic collection
    JavaScriptSerializer jss = new JavaScriptSerializer();
    // write the result out to the HTTP Stream (HtmlTextWriter in overridden Render method)
    
     writer.Write(jss.Serialize(allEmployees));
    

    You will see JSON on the client side which looks like the following:

    [
        {
          F_Name: "Seth",
          L_Name: "Godin"
        },
        {
           F_Name: "Charles",
           L_Name: "Petzold"
        },
        {
           F_Name: "Jeff",
           L_Name: "Prosise"
        }
    ]
    

    Finally, you may question why I want to do this. The simple answer is that I want to new type which is only used to serialize to send to my client. A great reason for an anonymous type and an anonymous collection of anonymous types, don't you think?

提交回复
热议问题