gpt4 book ai didi

c# - linq 查询可能缺少一些 from 值

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:26 24 4
gpt4 key购买 nike

我正在从 JSON 中提取联系人,Data 中的联系人可能包含电子邮件和电话号码。

如果他们执行以下查询,则完美无缺:

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's 的情况下做到这一点,从而使代码更短更简洁?

这是一个缺少电子邮件的 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 反序列化成什么,我只能在这里猜测一个解决方案,但我认为如果你在你的 Emails 上使用 DefaultIfEmpty() code> 和 Phones 集合,然后在 EmailValuePhoneNumber 上使用 null 条件运算符,你应该得到你想要的结果:

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 查询可能缺少一些 from 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com