Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Anonymous Array of Anonymous Objects from loop
    primarykey
    data
    text
    <p>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.</p> <p>I know I can make an anonymous array of anonymous object manually like this</p> <pre><code>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" } }; </code></pre> <p>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.</p> <pre><code>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 &amp;&amp; list.Length &gt; 0) { List&lt;DupeReturn&gt; dupes = new List&lt;DupeReturn&gt; { }; 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); } } </code></pre> <p>I really don't want to have to make that additional private class if I don't have to. Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload