需要在Linq

时间:2017-12-04 07:00:44

标签: c# linq json.net

我使用此代码提取电子邮件地址列表并搜索特定地址,如果它是重复的,那么我需要提取" id" (值以"引导_"开头),它位于Datam类和/或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)

所以我有我的电子邮件列表,但如何将ID附加到他们身上,我想我需要创建一个匿名类型?

我已经尝试了这个,但无法弄清楚如何回到根

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);
    }

1 个答案:

答案 0 :(得分:2)

在这种情况下,我发现ShortcutsEnabled比流畅的语法更方便,正是因为在嵌套的 angular.module('myApp', ['ui.router']) .config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('foo', { url:'foo', views: { ... } }) .state('foo.foo1', { url:'foo1', views: { ... } }) .state('bar', { url:'bar', views: { ... } }) .state('bar.bar1', { url:'bar1', views: { ... } }) $urlRouterProvider.otherwise('/'); }); 查询中,可以访问外部项而无需手动创建中间匿名类型以将值传递到内循环。

因此:

SelectMany

这避免了需要上升&#34;从内部项目到外部项目。

示例query syntax