我从JSON中提取联系人,数据中有联系人,其中可能包含电子邮件和电话号码。

如果他们这样做,则以下查询将完美工作:

var results = from d in json.Data
                from c in d.Contacts
                from e in c.Emails
                from p in c.Phones
                select new IoModel {Email = e.EmailValue, Id = d.Id, FullName = string.IsNullOrEmpty(c.Name) ? c.Name : d.DisplayName, Phone = p.PhoneNumber, DOB = d.CustomDOB};


如果数据没有电子邮件或电话,则它会正常运行,但不会返回联系人姓名。

有没有一种方法可以在不使用if和and的情况下使代码更短,更清晰?

这是缺少电子邮件的JSON的示例:

{
    "data": [
        {
            "addresses": [],
            "contacts": [
                {
                    "created_by": "user_ltUL3eXGPRWb5ghDeGTfOe9qjW0LeE2e4ouopLcSSWj",
                    "date_created": "2017-12-28T17:13:00.392000+00:00",
                    "date_updated": "2017-12-28T17:13:58.453000+00:00",
                    "emails": [],
                    "id": "cont_6hxxhz51ctlTnHfo8gA8cce0rthS1dJy1kguKAj148s",
                    "integration_links": [
                        {
                            "name": "LinkedIn Search",
                            "url": "https://www.linkedin.com/search/results/index/?keywords=John%20Doe"
                        }
                    ],
                    "lead_id": "lead_3UzfxCgQHmw4BlGwElitHhlP6E7q9Tg3sdSkTl1CIXp",
                    "name": "John Doe",
                    "organization_id": "orga_PvgGx1opSZDsCHl73P8OoSFZUlJ3qsNGI2kwgoObM17",
                    "phones": [
                        {
                            "phone": "+15555555555",
                            "phone_formatted": "+1 555-555-5555",
                            "type": "mobile"
                        }
                    ],
                    "title": "Main Service",
                    "updated_by": "user_ltUL3eXXPRWb5shDeGTfOe9qjP0LeE2e4ouopLcSSWj",
                    "urls": []
                }
            ],
            "created_by": "user_ltUL3eXXPRWb5shDeGTfOe9qjW0LeE2e4ouopLcSSWP",
            "created_by_name": "John",
            "custom": {
                "Date Of Birth": "1901-08-01",
                "Department": "Main Service",
                "Initial Service": "Main Service"
            },
            "custom.lcf_ufMH5ZhlR99zcdvJxKMxdgxcIbV4wtgTb3EdWDEkL8g": "1971-08-17",
            "date_created": "2017-12-28T17:13:00.388000+00:00",
            "date_updated": "2017-12-29T21:41:53.840000+00:00",
            "description": "",
            "display_name": "John Doe",
            "html_url": "https://app.close.io/lead/lead_3UzfxCgQHmw4BlGwElitHhlP6E7q9Tg3sdSkTl1CIXp/",
            "id": "lead_3UzfxCgQHmw4BlGwElitHhlP5E7q9Tg3sdSkTl1CIXp",
            "integration_links": [
                {
                    "name": "Google Search",
                    "url": "http://google.com/search?q=John%20Doe"
                }
            ],
            "name": "",
            "opportunities": [],
            "organization_id": "orga_AvcGx4opSZDsCHl73H8OogDDUlJ3qsNGI2kwgoObA17",
            "status_id": "stat_LpJu8FO72WgIob4qDDVnS4GEieoU41zmQ8xBquTvusm",
            "status_label": "Established Patient",
            "tasks": [],
            "updated_by": "user_0JFRnl8QRvRhMhAlLz4JJxgmrzPeLs3xboxYyj5Pm80",
            "updated_by_name": "BT",
            "url": null
        }
    ],
    "has_more": false,
    "total_results": 1
}

最佳答案

由于我不知道将JSON反序列化为什么,因此我只能在这里找到解决方案,但是我认为如果您在DefaultIfEmpty()Emails集合上使用Phones,然后使用空条件运算符在EmailValuePhoneNumber上,应该得到想要的结果:

var results = from d in json.Data
              from c in d.Contacts
              from e in c.Emails.DefaultIfEmpty()
              from p in c.Phones.DefaultIfEmpty()
              select new IoModel
              {
                  Email = e?.EmailValue,
                  Id = d.Id,
                  FullName = string.IsNullOrEmpty(c.Name) ? c.Name : d.DisplayName,
                  Phone = p?.PhoneNumber,
                  DOB = d.CustomDOB
              };

关于c# - linq查询中可能缺少一些值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48051862/

10-12 02:48