Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
So I have below code running, where I am calling an Api, which gives me result a Json Array which I am later querying in my code. The response, I get from the api is mostly constant and since it has SOME data, it takes few seconds to get the complete result. I would like to know, if I can store the result coming from api in some way in cache in order to make the whole processing part faster?
EDIT: below code is inside a Foreach loop
foreach (var did in myDeserializedClass)
{
if (did.id == matchedid)
{
url = "https://.../api/information/test";
var response = await Request(Stream.Null, url, "GET");
string message = await response.Content.ReadAsStringAsync();
var allcodes = JsonConvert.DeserializeObject<List<CoCode>>(message);
foreach(var cc in allcodes )
{....// rest of my code
You can absolutely use a memory cache, even Microsoft provide one which if possible also can be place in the api side for quicker lookups
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.memory.memorycache?view=dotnet-plat-ext-6.0
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have the following
variableAs = "A12,B12,C12"
variableBs = "1.54,75.30,55.50"
method (HashSet<string> variableAs, HashSet<double> variableBs)
foreach (var variableA in variableAs)
{
Method here requires the two values, must have preserved datatype
and be in same order, ie A12 with 1.54, B12 with 75.30
}
I have tried zip from this answer but I do not know how it works with hashset arrays,
NOTE the Method i am editing has it has hashset, the actual values are for example only, if there is an error, It must be my understanding of what a hashset is but I cannot change the hashset
You need to first populate your input-strings into some collection:
var As = variableAs.Split(',');
var Bs = variableBs.Split(',');
However a HashSet is not an ordered collection and thus the wrong choice here. You'd need a List or just an array.
Now you may use the mentioned Zip-function to combine the two elements together:
var result = As.Zip(Bs, (a, b) => new { a, b });
See the complete code at this fiddle: https://dotnetfiddle.net/9qTG2E
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have the following code :
string messageString1 = JsonConvert.SerializeObject(thisComputer);
var data = new { deviceid = "info1", devicetype = "info2", data = messageString1};
My goal is to add messageString1 into data .
thisComputer is a class and i know this part of the code works , because i got it working before , i'm just not being capable to get messageString1 into data.
I am not being able to insert data into data.I have tried several different ways but i still haven't figure it out.
EDIT:
the problem is that i am trying to send the JSON to azure IoThub and in fact the contents are getting trough and into IoT Hub but all the double quotes characters are now '\"' and that constitutes the problem .
EXAMPLE: If the data inside is :
{"data":"dataInfo"}
in Iot Hub i see :
{\"data\":\datainfo\"}
Thanks in advance.
Currently, you're JSON-encoding an object as a string, and then when you're sending the anonymous type instance to Cloud IoT, that's applying JSON encoding again.
It looks like you don't want the value of data to be a string - you want it to be the data from thisComputer. So just avoid that first level of encoding:
var data = new { deviceid = "info1", devicetype = "info2", data = thisComputer };
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm posting this so that the code is available to whomever finds it helpful.
Slightly reduced code based on Noctis's suggestion.
StringBuilder DescriptionText = new StringBuilder();
async void RunDescription(StringBuilder description)
{
DescriptionText = description;
await Task.Delay(1000); // Short delay before the text starts printing so the window has time to load
new Thread(AddTextToTextBlock).Start();
}
void TextBlockDispatcher(string text)
{
TextBlock1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => TextBlock1.Inlines.Add(text)));
}
void AddTextToTextBlock()
{
foreach (char c in DescriptionText.ToString())
{
Thread.Sleep(30);
TextBlockDispatcher(c.ToString());
}
}
Sounds like the usual hoops you need to jump through. The only thing i might consider changing is instead of calling the dispatcher, and then checking if it has access, simply calling it directly on your object.
It should look kinda like :
TextBlock1.Dispatcher.BeginInvoke((Action)(() => /* logic here */ )
Saves you an call, but same same really.
You'll need to massage it in, since this is from the top of my head, but it should point you in the right direction.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am making a game where people listen to determine whether something is black or white. I want to store how many times they got it correct and how many times they got it wrong in a database so they can see the old results.
What kind of database (like Sqlite) should I use? How should I save this information and display it later?
You can use BinarySerializtion, XMLSerialization,......
so first create a class:
[Serializable]
public class Result
{
Public int TimesRight {get;set;}
Public int TimesWrong {get;set;}
}
now in your game:
Result result = new Result();
result.TimesRight = x; //value of times right
result.TimesWrong = y; //value of times wrong
Now choose the way you want to save it, here are some references for Binary and XML serialization:
Binary Serialization
XML Serialization
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm looking for a C# "one-liner" (need not strictly be a single line, but very short is preferable) way to download an RSS feed from a given HTTP URL, and extract specific data. Robustness be damned. Something that doesn't require any external libraries.
Specifically I want to count the number of <item>s in the RSS. But some kind of LINQ method that could be reused to, say for example, return a list of the item <title> elements would be most useful, if it can be kept short.
Regex.Matches(new WebClient().DownloadString("http://stackoverflow.com/feeds/question/7180063"), "<entry>").Count
What about something like this:
var rssFeed = XDocument.Load("http://weblogs.asp.net/scottgu/rss.aspx");
var posts = from item in rssFeed.Descendants("item")
select new
{
Title = (string)item.Element("title"),
Published = (DateTime?)item.Element("pubDate"),
Url = (string)item.Element("link"),
};
Source.
SyndicationFeed.Load(XmlReader.Create("http://weblogs.asp.net/scottgu/rss.aspx")).Items.Count();