vipergtsrz
vipergtsrz

Reputation: 1061

C# Anonymous Array of Anonymous Objects from loop

I have a web service I'm calling that does a dupe check based on email, firstName and lastName. The object I am getting back from the business layer is very large and has way more data than I need to pass back. In my web service function, I would like to only pass back 10 fields via JSON. Instead of making a new class with those 10 fields, I was looking to loop through my large return object and just make a list or array of anonymous objects with those 10 fields in it instead.

I know I can make an anonymous array of anonymous object manually like this

obj.DataSource = new[]
{
    new {  Text = "Silverlight",  Count = 10,  Link = "/Tags/Silverlight"  },
    new {  Text = "IIS 7",        Count = 11,  Link = "http://iis.net"     }, 
    new {  Text = "IE 8",         Count = 12,  Link = "/Tags/IE8"          }, 
    new {  Text = "C#",           Count = 13,  Link = "/Tags/C#"           },
    new {  Text = "Azure",        Count = 13,  Link = "?Tag=Azure"         } 
};

My issue is that I want to do that exact thing except by looping through my big object and only pulling out the fields I need to return.

private class DupeReturn
{
    public string FirstName;
    public string LastName;
    public string Phone;
    public string Owner;
    public string Address;
    public string City;
    public string State;
    public string Zip;
    public string LastModified;
}

[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CheckForDupes(string Email, string FirstName, string LastName)
{
    contact[] list = Services.Contact.GetDupes(Email, FirstName, LastName);
    if (list != null && list.Length > 0)
    {
        List<DupeReturn> dupes = new List<DupeReturn> { };
        foreach (contact i in list)
        {
            DupeReturn currentObj = new DupeReturn
            {
                FirstName = i.firstname,
                LastName = i.lastname,
                Phone = i.telephone1,
                Owner = i.ownerid.ToString(),
                Address = i.address1_line1,
                City = i.address1_city,
                State = i.address1_stateorprovince,
                Zip = i.address1_postalcode,
                LastModified = i.ctca_lastactivityon.ToString()
            };
            dupes.Add(currentObj);
        }
        return Newtonsoft.Json.JsonConvert.SerializeObject(dupes);    
    }
}

I really don't want to have to make that additional private class if I don't have to. Any help would be appreciated.

Upvotes: 3

Views: 3874

Answers (1)

Oded
Oded

Reputation: 498992

Using LINQ you can create a list of your anonymous type.

var dupes = list.Select(i => new { FirstName = i.firstname,
                                   LastName = i.lastname,
                                   Phone = i.telephone1,
                                   Owner = i.ownerid.ToString(),
                                   Address = i.address1_line1,
                                   City = i.address1_city,
                                   State = i.address1_stateorprovince,
                                   Zip = i.address1_postalcode,
                                   LastModified = i.ctca_lastactivityon.ToString()
                                    });

Upvotes: 6

Related Questions