nightowl
nightowl

Reputation: 339

Match and Map fields from two lists LINQ

I have two separate API requests. The first one gives me a JSON List of Mailshake campaigns.

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

In turn, I get this response.

{
"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": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "fromName": "xxxxx xxxx",
            "created": "2018-09-24T11:53:48.961Z"
        },
        "url": "https://mailshake.com/app/#/172/campaigns/3/overview"
    },...
}

I store this response in OpenResponseViewModel

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

then I filter it out into Response Model as an element in list for every campaign.

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

Simular for the second call to the API I get all open emails as 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();

So basically I get two lists of objects.

CampaingId: 360213
RecipientEmailAddress : null
SenderEmailAddress : [email protected]

CampaingId: 358593
RecipientEmailAddress : null
SenderEmailAddress : [email protected]

and

CampaingId: 358593
RecipientEmailAddress : [email protected]
SenderEmailAddress : null 

CampaingId: 360213
RecipientEmailAddress : [email protected]
SenderEmailAddress : null

What I need is match the CampaingId from the first list to the CampaingId from the second list into 3rd list to get the SenderEmailAddress.

and get a list of objects like this

CampaingId: 358593
RecipientEmailAddress : [email protected]
SenderEmailAddress : [email protected]

CampaingId: 360213
RecipientEmailAddress : [email protected]
SenderEmailAddress : [email protected]

how do i do this witl LINQ

Upvotes: 0

Views: 257

Answers (1)

Magnus
Magnus

Reputation: 46947

Join the lists:

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

Upvotes: 1

Related Questions