sparagas
sparagas

Reputation: 49

How to take variable and use at other method

string.Join(", ", CampaignManagementService.GetAdvertisers((string) Session["ticket"]).Select(kv => kv.Key.ToString()).ToArray())

From this method I get ID's but I don't know how to use those in another method for example:

String.Join(", ", CampaignManagementService.GetCampaigns((string)Session["ticket"] , (int) 16156).Select(x => x.Key + " - " + x.Value));

In this method I need ID | (int) 16156 | but this is only static ID value I want to put ID from first method

Can anyone help me ?

Upvotes: 2

Views: 135

Answers (3)

SWeko
SWeko

Reputation: 30872

I'll assume several things using my psychic debugging powers:

  1. CampaignManagementService.GetAdvertisers takes a string and returns a Dictionary<int, Advertiser>

  2. The key of the dictionary is the ID of the advertiser returned.

  3. You do the string.Join(...) on that so you'll get a comma separated list that is easy to transfer to the client and display there

  4. You need to somehow get all campaigns for all advertisers you've gotten from the dictionary.

  5. The campaigns for each advertiser are stored in a Dictionary<int, string> where the key is the ID of the campaign and the value is, lets say, the name of the campaign

Under those assumptions:

var ticket = (string) Session["ticket"];
var advertisers = CampaignManagementService.GetAdvertisers(ticket);

will give you the advertisers dictionary

var advertiserIDs = advertisers.Select(kv => kv.Key)
var strAdvertiserIDs = advertisers.Select(kv => kv.Key.ToString())

and will give you, respectively, an IEnumerable<int> and a IEnumerable<string> that will contain the advertiser IDs

var csvAdvertisers = string.Join(", ", strAdvertiserIDs)

will give you the original comma-separated list of IDs (no .ToArray() is necessary, since string.Join has an overload that accepts an IEnumerable<string>).
Next, to get all the campaings you can use the SelectMany method, like this:

var campaignsKvp = advertiserIDs.SelectMany(id =>
                             CampaignManagementService.GetCampaigns(ticket, id))

That will give you an IEnumerable<KeyValuePair<int, string>> which is not a really good data structure, but you can transform it using:

var campaigns = campaignsKvp.Select(kvp => kvp.Key.ToString() + "-"+kvp.Value);
var csvCampaigns = string.Join(", ", campaigns)

Upvotes: 1

Dustin Kingen
Dustin Kingen

Reputation: 21245

You should transform the output into a readable format when you're about to display it to the user. I would suggest to avoid transforming your data until you need to.

public List<Advertiser> GetAdvertisers()
{
    return CampaignManagementService.GetAdvertisers((string) Session["ticket"]).ToList();
}

public List<Campaign> GetCampaigns(IEnumerable<Advertiser> advertisers)
{
    return advertisers.Select(a => CampaignManagementService.GetCampaigns((string)Session["ticket"],
                                   Convert.ToInt32(a.Key))).ToList();
}

Upvotes: 1

Pragmateek
Pragmateek

Reputation: 13354

Maybe something like that:

CampaignManagementService.GetAdvertisers((string) Session["ticket"])
                         .SelectMany(kv => CampaignManagementService.GetCampaigns((string)Session["ticket"] , kv.Key))
                         .Select(x => x.Key + " - " + x.Value)

Upvotes: 2

Related Questions