3

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.

2
  • 1
    What have you got against making another class?? Commented Apr 18, 2012 at 13:04
  • 1
    It's not my application and I wanted to just use a disposable class because it's only being used for this one function and no where else. Commented Apr 20, 2012 at 18:47

1 Answer 1

6

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()
                                    });
Sign up to request clarification or add additional context in comments.

3 Comments

If you use Linq, you can use the Take() and Skip() functions for paging.
@LajosArpad - True, though I don't see what that's got to do with the question.
I don't think this does what I was asking. I want to do know how to make an anonymous list from inside a loop. So I want to create the empty list and then add objects to that list from inside the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.