【问题标题】:Match and Map fields from two lists LINQ来自两个列表 LINQ 的匹配和映射字段
【发布时间】:2019-08-13 09:45:35
【问题描述】:

我有两个单独的 API 请求。 第一个给了我一个 Mailshake 活动的 JSON 列表。

var requestCampaings = new RestRequest("/2017-04-01/contolerr/method", Method.GET);

反过来,我得到了这个响应。

{
"nextToken": "1545314518|240060",
"results": [
    {
        "object": "campaign",
        "id": 36,
        "title": "Email Part 2",
        "created": "2019-08-12T11:50:01.442Z",
        "archived": null,
        "isPaused": false,
        "messages": [
            {
                "object": "message",
                "id": 7764,
                "type": "initial",
                "subject": "chat",
                "replyToID": null,
                "isPaused": false
            }
        ],
        "sender": {
            "object": "sender",
            "id": "359-false",
            "emailAddress": "sxxxxxxxxxt@xxxxx.com",
            "fromName": "xxxxxx xxxxx",
            "created": "2019-05-13T14:53:49.615Z"
        },
        "url": "https://mailshake.com/app/#/17032/cont/360213/overview"
    },
    {
        "object": "campaign",
        "id": 359073,
        "title": "TEST xxxxxx",
        "created": "2019-08-09T09:16:42.286Z",
        "archived": null,
        "isPaused": false,
        "messages": [
            {
                "object": "message",
                "id": 774276,
                "type": "initial",
                "subject": "Test Test",
                "replyToID": null,
                "isPaused": false
            }
        ],
        "sender": {
            "object": "sender",
            "id": "24121-28668-false",
            "emailAddress": "xxxxxxxxx@xxxxxx.xx.xx",
            "fromName": "xxxxx xxxxxx",
            "created": "2018-09-24T11:53:48.961Z"
        },
        "url": "https://mailshake.com/app/#/1/campaigns/73/overview"
    },
    {
        "object": "xxxxx",
        "id": 35,
        "title": " Outreach",
        "created": "2019-08-08T16:34:23.76Z",
        "archived": null,
        "isPaused": true,
        "messages": [],
        "sender": {
            "object": "sender",
            "id": "24121-28668-false",
            "emailAddress": "xxxxx@xxxx.xx.xxx",
            "fromName": "xxxxx xxxx",
            "created": "2018-09-24T11:53:48.961Z"
        },
        "url": "https://mailshake.com/app/#/172/campaigns/3/overview"
    },...
}

我将此响应存储在 OpenResponseViewModel 中

var campaings = JsonConvert.DeserializeObject<OpensResponseViewModel>(contentCampaings).Results;

然后我将其过滤到响应模型中,作为每个活动的列表中的一个元素。

var campaingsIds = campaings.Select(a => new Response() { CampaignId = a.Id, SenderEmail = a.Sender.EmailAddress }).ToList();

第二次调用 API 时类似,我将所有打开的电子邮件都作为 JSON。

var info = JsonConvert.DeserializeObject(contentOpens).Results;

var emailClicked = info.Where(x=>x.IsDuplicate==false).Select(a => new Response() { CampaignId = a.Campaign.Id, RecipientIEmail = a.Recipient.EmailAddress }).ToList();

所以基本上我得到了两个对象列表。

CampaingId: 360213
RecipientEmailAddress : null
SenderEmailAddress : sender360213@mail.com

CampaingId: 358593
RecipientEmailAddress : null
SenderEmailAddress : sender358593@mail.com

and

CampaingId: 358593
RecipientEmailAddress : recipient@mail.com
SenderEmailAddress : null 

CampaingId: 360213
RecipientEmailAddress : recipient@mail.com
SenderEmailAddress : null

我需要将第一个列表中的 CampaingId 与第二个列表中的 CampaingId 匹配到第三个列表中,以获取 SenderEmailAddress。

并获得这样的对象列表

CampaingId: 358593
RecipientEmailAddress : recipient@mail.com
SenderEmailAddress : sender358593@mail.com

CampaingId: 360213
RecipientEmailAddress : recipient@mail.com
SenderEmailAddress : sender360213@mail.com

我如何用 LINQ 做到这一点

【问题讨论】:

    标签: c# json linq api


    【解决方案1】:

    加入列表:

    var q = from c in campaingsIds
            join e in emailClicked on c.CampaingId equals e.CampaingId
            select new Response()
            {
                CampaingId = c.CampaingId,
                RecipientEmailAddress = e.RecipientEmailAddress,
                SenderEmailAddress = c.SenderEmailAddress
            };
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 2020-04-28
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 2018-03-01
      相关资源
      最近更新 更多