I have the client below for a Web Api application, inside this client I have a dictionary with multiple dependencies. I would like to print to the log file the a line with the key and multiple values. as the dictionary is in a separeted class I can not reach the values correctly. This is the input:
[{
"Inputs": [{
"Input": "A B C"
}, {
"Input": "B C E "
}, {
"Input": "C G"
}, {
"Input": "D A F"
}, {
"Input": "E F"
}, {
"Input": "F H "
}]
}]
this is my DataReader class where the dictionary is:
public class DataReader
{
public Dictionary<char, char[]> Read()
{
JArray data;
using (StreamReader sr = new StreamReader(@"C:input.json"))
{
data = (JArray)JToken.ReadFrom(new JsonTextReader(sr));
}
var values = data.SelectTokens("..Input").Select(x => x.ToString()).ToArray();
var dict = values.ToDictionary(s => s[0], v => v.Substring(2).Where(Char.IsLetter).ToArray());
IEnumerable<char> Dependencies(char input)
{
yield return input;
if (dict.TryGetValue(input, out char[] childDepends))
{
foreach (char result in childDepends.SelectMany(Dependencies))
{
yield return result;
}
}
}
foreach (char s in values.Select(x => x[0]))
{
Console.Write(s); Console.Write(" ");
//the line below calls the IEnumerable Dependencies:
Console.WriteLine(String.Join(" ", dict[s].SelectMany(Dependencies).Distinct().OrderBy(x => x)));
}
return dict;
this is my program.cs class where I need to call the dictionary and print the values:
class Program : ApiController
{
static void Main(string[] args)
{
RunAsync().Wait();
Console.ReadLine();
}
DataReader reader;
public Program()
{
reader = new DataReader();
}
static async Task RunAsync()
{
DataReader reader = new DataReader();
var data = reader.Read();
foreach (var key in data.Keys)
{
foreach (var s in data.Values)
{
//assembly of values to send
var transactionId = Guid.NewGuid();
string userName = "test";
string message = string.Join(" ", "<< Dependencies for", key, ">>",s[0]);
// write the file here
var logLine = string.Join(",", transactionId, message);
using (StreamWriter writer = File.AppendText(@"C:\log.txt"))
{
writer.WriteLine(logLine);
}
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:56047/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var payload = new ProcessorDto();
payload.UserName = userName;
payload.Message = message;
var result = await client.PostAsync("api/validate", payload, new JsonMediaTypeFormatter());
var responseData = await result.Content.ReadAsStringAsync();
// update log with response:
var fileText = File.ReadAllText(@"log.txt");
var updatedLine = logLine + "," + responseData;
fileText = fileText.Replace(logLine, updatedLine);
File.WriteAllText(@"C:\log.txt", fileText);
}
}
}
}
the code above prints the below to the console:
A B C E F G H
B C E F G H
C G
D A B C E F G H
E F H
F H
this part of my code needs to be modified as it is writing the log.txt incorrectly:
string message = string.Join(" ", "<< Dependencies for", key, ">>",s[0]);
I am not sure what to add on the s[0] as it is returning the below:
23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for A >> B,"False"
c5f778d8-6eac-4508-b01f-864f4a49f57a,<< Dependencies for A >> C,"False"
8d5821b3-2f68-40fd-a18c-473d6aa222ad,<< Dependencies for A >> G,"False"
8e693ea8-e97b-4e6a-831b-c0348d918e29,<< Dependencies for A >> A,"False"
af7a4497-8c18-401b-b287-b05abb94f5e8,<< Dependencies for A >> F,"False"
ecad1901-c503-488a-a5f2-6ec04d5ac7a9,<< Dependencies for A >> H,"False" 50d4edb9-770b-4c6d-be54-9f738cde6dd1, << Dependencies forB>>C,"False"
208c8ae4-e669-4f06-8548-c1035c22ff61, << Dependencies forB>>G,"False"
e001b386-02c0-4111-a715-c4d524074be4, << Dependencies forB>>A,"False"
19fbe253-284b-4221-a762-e2eedb9b2d73, << Dependencies forB>>F,"False"
e100ce7f-be94-4fc5-a592-f4088a031b6d, << Dependencies forB>>H,"False"
e645a38c-a33d-428d-90be-d6f34d27c935, << Dependencies forC>>B,"False"
07031b1a-8546-4f9e-8f49-78a6d8ef1bfa, << Dependencies forC>>C,"False"
3d9de182-9c32-4716-bff9-0840c176cec6, << Dependencies forC>>G,"False"
623c4529-b025-4e4d-abb3-98ea21e40381, << Dependencies forC>>A,"False"
ee46a0ae-2518-4848-9d87-9fb554d527de, << Dependencies forC>>F,"False"
a0769061-2c72-44cd-a174-ee11766bb920, << Dependencies forC>>H,"False"
311be8b8-93fe-420d-b0ae-6b74af3291e2, << Dependencies forD>>B,"False"
f7304991-51fc-42b1-bd31-749c6fde77d3, << Dependencies forD>>C,"False"
c59163c8-d4bb-4720-845c-2e1111d2cde5, << Dependencies forD>>G,"False"
cd7869bf-5427-470e-a064-6bc785867eb3, << Dependencies forD>>A,"False"
7f96e0cf-764e-4803-a70a-b28e8da8f85e, << Dependencies forD>>F,"False"
65c5ac2f-22eb-4e84-9b8d-35c1e0e4d862, << Dependencies forD>>H,"False"
0ffeaad0-1626-43b3-9e45-050d62023e69, << Dependencies forE>>B,"False"
3a7e2625-4e27-4f33-af11-c75fb7568560, << Dependencies forE>>C,"False"
cb9f2d26-bd62-4200-a7aa-74190d124389, << Dependencies forE>>G,"False"
6e063ec1-c82d-4a84-85f1-2f8baba281c6, << Dependencies forE>>A,"False"
63895dc3-de7b-42f6-b0e3-fdc429ae5fc3, << Dependencies forE>>F,"False"
a1626f22-b476-4d00-a08c-aab08b07e5ef, << Dependencies forE>>H,"False"
faa2dc3c-3303-46d0-8f68-2ac1272504d1, << Dependencies forF>>B,"False"
031e5115-30ed-4ada-9db2-729df8dd7897, << Dependencies forF>>C,"False"
90577a7a-cc12-4a57-a9de-97b93b59398f, << Dependencies forF>>G,"False"
2b668cb0-5d6a-4b7c-8326-c8cf39436bbf, << Dependencies forF>>A,"False"
c15b1c49-534f-40e6-8f10-c16cd66a6cb2, << Dependencies forF>>F,"False"
4aabfcc0-dfc7-45a2-b346-e7fade4f48ff, << Dependencies forF>>H,"False"
but it should be as below:
23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for A >> B, C, E, F, G, H,"False" 23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for B >> C, E, F, G, H "False" 23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for C >> G "False" 23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for D >> A, B, C, E, F, G, H "False" 23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for E >> F, H "False" 23b3c058-8ec0-4925-9e42-ae5de2863ca7,<< Dependencies for F >> H "False"
What should I add to the code below to print the correct output
string message = string.Join(" ", "<< Dependencies for", key, ">>",s[0]);