1

I'm using this code to pull a list of email addresses and search them for a specific address, if it's a duplicate then I need to pull the "id" (the value starts with "lead_") which is in the Datam class and/or Contacts

    var emails = json
        // Enumerate through all data
        .Data
        // Enumerate through all contacts of all data
        .SelectMany(d => d.Contacts)
        // Enumerate through all emails of all contacts
        .SelectMany(c => c.Emails)
        // Get email value
        .Select(e => e.EmailValue)

So I have my list of emails, but how do I attach the ID's to them, I assume I need to create an anonymous type?

I've tried this but can't figure out how to go back to the root

JSON:

{
    "data": [
        {
            "addresses": [], 
            "contacts": [
                {
                    "created_by": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
                    "date_created": "2017-12-03T05:42:30.737000+00:00", 
                    "date_updated": "2017-12-03T05:45:32.227000+00:00", 
                    "emails": [
            {
                            "email": "testemail@testemail.com", 
                            "type": "office"
                        }
            ], 
                    "id": "cont_kH9tcx970T3ZTGKPPP2vh5u3Ha8P9Rjz6gGqflgzwvy", 
                    "integration_links": [
                        {
                            "name": "LinkedIn Search", 
                            "url": "https://www.linkedin.com/search/results/index/?keywords=Test%20Woodard"
                        }
                    ], 
                    "lead_id": "lead_12Rz7R2TrWmwUhCGVTRh8rt6A8hokaXgujSEPlfoWpD", 
                    "name": "Test User", 
                    "organization_id": "orga_iFnIMLDbvGevDrXMHYQSbYnLu1GjrdM2TEWWsCoraJH", 
                    "phones": [
                        {
                            "phone": "+15558675309", 
                            "phone_formatted": "+1 555-867-5309", 
                            "type": "office"
                        }
                    ], 
                    "title": "", 
                    "updated_by": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
                    "urls": []
                }
            ], 
            "created_by": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
            "created_by_name": "Test User", 
            "custom": {
                "Date Created": "2017-12-03", 
                "Initial Service": "Men's Health", 
                "Lead Owner": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
                "Marketing Source": "Bing"
            }, 
            "custom.lcf_UfNWMxg2f7UU28rI9RAhTEAPWMyXU1UFZb5oNJEgR0K": "Bing", 
            "custom.lcf_dDtwGb41tpi8XkVMMgxdHxEJ92A7ujreRU1aPPZd95B": "Chiropractor", 
            "custom.lcf_fUNjcSq8bemwdQL0wogc3wgyxmz3ZD17fKdv8s4wkWV": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
            "custom.lcf_y37bo72LesrOBWuVPSoRfIazw5KflujnV81nGCfcjZ3": "2017-12-03", 
            "date_created": "2017-12-03T05:42:30.733000+00:00", 
            "date_updated": "2017-12-03T05:45:32.234000+00:00", 
            "description": "", 
            "display_name": "test", 
            "html_url": "https://app.close.io/lead/lead_12Rz7R2TrWmwUhCFVTRh8rt6A8hokaXgujSEPlfoWpG/", 
            "id": "lead_12Rz7R2TrWmwUhCFVTRh8rt6A8hokaXgujSEPlfoWpD", 
            "integration_links": [
                {
                    "name": "Google Search", 
                    "url": "http://google.com/search?q=test"
                }
            ], 
            "name": "test", 
            "opportunities": [
                {
                    "confidence": 50, 
                    "contact_id": null, 
                    "contact_name": null, 
                    "created_by": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
                    "created_by_name": "Test User", 
                    "date_created": "2017-12-03T05:44:31.131000+00:00", 
                    "date_lost": null, 
                    "date_updated": "2017-12-03T05:44:31.131000+00:00", 
                    "date_won": null, 
                    "id": "oppo_xQ5S1dHMjDWoeUBAigtyiwuwFUkxtbxHURQX5UaL7fT", 
                    "integration_links": [], 
                    "lead_id": "lead_12Rz7R2TrWmwUhCFVTRh8rt6A8hokaXgujSEPlfoWpD", 
                    "lead_name": "test", 
                    "note": "New Lead", 
                    "organization_id": "orga_iFnIMLDbvNevDrXMHYQSbYnLu1GjrdM2TEWWsCoraJH", 
                    "status_id": "stat_SwQmzqoIqIt20j9YBDxIYem11GyZPGL30G7PdkWYdvG", 
                    "status_label": "Chiropractor", 
                    "status_type": "active", 
                    "updated_by": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
                    "updated_by_name": "Test User", 
                    "user_id": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
                    "user_name": "Test User", 
                    "value": 140000, 
                    "value_currency": "USD", 
                    "value_formatted": "$1,400", 
                    "value_period": "one_time"
                }
            ], 
            "organization_id": "orga_iFnIMLDbvNevDrXMHYQSbYnLu1GjrdM2TEWWsCoraJH", 
            "status_id": "stat_auhCEJZNhaUnX9DHdrCaZ5r5mhpqxjfaE1hdwnPz6xx", 
            "status_label": "Potential", 
            "tasks": [], 
            "updated_by": "user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG", 
            "updated_by_name": "Test User", 
            "url": null
        }
    ], 
    "has_more": false, 
    "total_results": 1
}


public partial class Welcome
    {
        [JsonProperty("data")]
        public Datum[] Data { get; set; }

        [JsonProperty("has_more")]
        public bool HasMore { get; set; }

        [JsonProperty("total_results")]
        public long TotalResults { get; set; }
    }
public partial class Datum
    {
        [JsonProperty("addresses")]
        public object[] Addresses { get; set; }

        [JsonProperty("contacts")]
        public Contact[] Contacts { get; set; }

        [JsonProperty("created_by")]
        public string CreatedBy { get; set; }

        [JsonProperty("created_by_name")]
        public string CreatedByName { get; set; }

        [JsonProperty("custom")]
        public Custom Custom { get; set; }

        [JsonProperty("custom.lcf_dDtwGb41tpi8XkVMMgxdHxEJ92A7ujreRU1aPPZd95B")]
        public string CustomInitialService { get; set; }

        [JsonProperty("custom.lcf_fUNjcSq8bemwdQL0wogc3wgyxmz3ZD17fKdv8s4wkWV")]
        public string CustomLeadOwner { get; set; }

        [JsonProperty("custom.lcf_UfNWMxg2f7UU28rI9RAhTEAPWMyXU1UFZb5oNJEgR0K")]
        public string CustomMarketingSource { get; set; }

        [JsonProperty("custom.lcf_y37bo72LesrOBWuVPSoRfIazw5KflujnV81nGCfcjZ3")]
        public string CustomDateCreated { get; set; }
        [JsonProperty("custom.lcf_bzaGzJw0MsgtffSeHDQhYjfBmacMWxCiPM3DlqOlYZQ")]
        public string CustomLocation { get; set; }
        [JsonProperty("custom.lcf_1EE18uGReRLF6xqI45kWA3ijfSWJTe718FSgTiXdyEC")]
        public string CustomStart { get; set; }

        [JsonProperty("custom.lcf_E23dCjch37Zushwm1ZzPm3JXKXKJMZgYEYBjx5oabDF")]
        public string CustomAppointment { get; set; }


        [JsonProperty("date_created")]
        public string DateCreated { get; set; }

        [JsonProperty("date_updated")]
        public string DateUpdated { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("display_name")]
        public string DisplayName { get; set; }

        [JsonProperty("html_url")]
        public string HtmlUrl { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("integration_links")]
        public IntegrationLink[] IntegrationLinks { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("opportunities")]
        public Opportunity[] Opportunities { get; set; }

        [JsonProperty("organization_id")]
        public string OrganizationId { get; set; }

        [JsonProperty("status_id")]
        public string StatusId { get; set; }

        [JsonProperty("status_label")]
        public string StatusLabel { get; set; }

        [JsonProperty("tasks")]
        public object[] Tasks { get; set; }

        [JsonProperty("updated_by")]
        public string UpdatedBy { get; set; }

        [JsonProperty("updated_by_name")]
        public string UpdatedByName { get; set; }

        [JsonProperty("url")]
        public object Url { get; set; }
    }
 public partial class Opportunity
    {
        [JsonProperty("confidence")]
        public long Confidence { get; set; }

        [JsonProperty("contact_id")]
        public object ContactId { get; set; }

        [JsonProperty("contact_name")]
        public object ContactName { get; set; }

        [JsonProperty("created_by")]
        public string CreatedBy { get; set; }

        [JsonProperty("created_by_name")]
        public string CreatedByName { get; set; }

        [JsonProperty("date_created")]
        public string DateCreated { get; set; }

        [JsonProperty("date_lost")]
        public object DateLost { get; set; }

        [JsonProperty("date_updated")]
        public string DateUpdated { get; set; }

        [JsonProperty("date_won")]
        public object DateWon { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("integration_links")]
        public object[] IntegrationLinks { get; set; }

        [JsonProperty("lead_id")]
        public string LeadId { get; set; }

        [JsonProperty("lead_name")]
        public string LeadName { get; set; }

        [JsonProperty("note")]
        public string Note { get; set; }

        [JsonProperty("organization_id")]
        public string OrganizationId { get; set; }

        [JsonProperty("status_id")]
        public string StatusId { get; set; }

        [JsonProperty("status_label")]
        public string StatusLabel { get; set; }

        [JsonProperty("status_type")]
        public string StatusType { get; set; }

        [JsonProperty("updated_by")]
        public string UpdatedBy { get; set; }

        [JsonProperty("updated_by_name")]
        public string UpdatedByName { get; set; }

        [JsonProperty("user_id")]
        public string UserId { get; set; }

        [JsonProperty("user_name")]
        public string UserName { get; set; }

        [JsonProperty("value")]
        public long Value { get; set; }

        [JsonProperty("value_currency")]
        public string ValueCurrency { get; set; }

        [JsonProperty("value_formatted")]
        public string ValueFormatted { get; set; }

        [JsonProperty("value_period")]
        public string ValuePeriod { get; set; }
    }

    public partial class Custom
    {
        [JsonProperty("Date Created")]
        public string DateCreated { get; set; }

        [JsonProperty("Initial Service")]
        public string InitialService { get; set; }

        [JsonProperty("Lead Owner")]
        public string LeadOwner { get; set; }

        [JsonProperty("Marketing Source")]
        public string MarketingSource { get; set; }

        [JsonProperty("Location")]
        public string Location { get; set; }
    }
    public class Email
    {
        [JsonProperty("email")]
        public string EmailValue { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }
    }

    public partial class Contact
    {
        [JsonProperty("created_by")]
        public string CreatedBy { get; set; }

        [JsonProperty("date_created")]
        public string DateCreated { get; set; }

        [JsonProperty("date_updated")]
        public string DateUpdated { get; set; }

        [JsonProperty("emails")]
        public Email[] Emails { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("integration_links")]
        public IntegrationLink[] IntegrationLinks { get; set; }

        [JsonProperty("lead_id")]
        public string LeadId { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("organization_id")]
        public string OrganizationId { get; set; }

        [JsonProperty("phones")]
        public Phone[] Phones { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("updated_by")]
        public string UpdatedBy { get; set; }

        [JsonProperty("urls")]
        public object[] Urls { get; set; }
    }
    public partial class Phone
    {
        [JsonProperty("phone_formatted")]
        public string PhoneFormatted { get; set; }

        [JsonProperty("phone")]
        public string PhoneNumber { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }
    }

    public partial class IntegrationLink
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("url")]
        public string Url { get; set; }
    }

    public partial class Welcome
    {
        public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
    }
prospector
  • 3,389
  • 1
  • 23
  • 40

1 Answers1

2

In cases like this I find the query syntax more convenient than the fluent syntax, precisely because in nested SelectMany queries the outer items are accessible without needing to manually create intermediate anonymous types for passing values into the inner loop.

Thus:

var query = from d in json.Data
            from c in d.Contacts
            from e in c.Emails
            select new { Email = e.EmailValue, LeadId = c.LeadId };
var emails = query.ToList();

This avoids the need to "go up" from the inner item to the outer.

Sample fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340