Read information from a dictionary [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I read information from a dictionary to a text box?
var cities = new Dictionary<int, string>
{
{ 10, "New York" },
};
textBox1.Text = cities.Values[1];

Try instead to use the index that you created on the dictionary:
var cities = new Dictionary<int, string>
{
{ 10, "New York" },
};
textBox1.Text = cities[10];

Related

insert a value to object add key was not worked [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Hi all I have a object like below
dynamic obj = new
{
Translations = new
{
test="",
}
};
In this code I need to add more like test="" in c#
expected output
dynamic obj = new
{
Translations = new
{
test="",
ds ="",
dsfd=""
}
};
How can I do that I tried with add key not worked
This work
dynamic obj = new
{
Translations = new Dictionary<string, string>
{
{ "test", "" },
}
};
obj.Translations.Add("ds", "");
obj.Translations.Add("dsfd", "");
This won't work
var obj = new
{
Translations = new
{
test="",
}
};
obj.Translations = new
{
test="",
ds="",
tedsfdst="",
}
Because anonymous type properties are read only and cannot be set. Also, you can't treat Translations as a dictionary and add new properties to it, unless you define it as dictionary.

How to update property inside a object in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I update "UserPhoneNumber" property value in "Data" property alone from this object. I have done only to change value of property and not nested object.
var updateProfile = generalSettingResponse.Select(x => settingsList.ContainsKey(x.Key) ? settingsList.First(y => y.Key.ToLower() == x.Key) : x);
sample json object:
{
"Name": "John",
"SNo": "1234",
"Data": {
"UserPhoneNumber": "9102287287",
"UserProfileName": "John"
}
}
If you want to change property of json object,here is a demo:
SampleJObject:
{ "Name": "John", "SNo": "1234", "Data": { "UserPhoneNumber": "9102287287", "UserProfileName": "John" } }
Use the following code,the property UserPhoneNumber will be changed:
SampleJObject["Data"]["UserPhoneNumber"] = "0123456789";
result:
You cannot use this:
updateProfile.Data.UserPhoneNumber = "+18888888888";
That will only work if the setters in the class are public.

delete empty entries tuple List String [closed]

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 2 years ago.
Improve this question
I have a Tuple List with 5 String values. This list contains about 250 entries with many empty entries.
How to delete these empty entries?
var program_list = new List<Tuple<string, string, string, string, string>>();
program_list.Add(Tuple.Create(program_name_String, publisher_name_String, program_version_String, install_location_String, uninstall_location_String));
I know how to delete these empty entries with a single String List. But this code won't work on Tuple List anymore.
program_names = program_names.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
program_names = program_names.Where(s => !string.IsNullOrEmpty(s)).Distinct().ToList();
program_names.Sort();
Thanks a lot.
I would suggest using a class rather than a tuple. Also you can still use it the same way you were using it for a string list. You just need to let it know to go deeper. Replace s with s.Item1 and you are set.
program_names = program_names.Where(s => !string.IsNullOrWhiteSpace(s.Item1)).Distinct().ToList();
but I suggest this:
public class myProgramClass
{
public string name, publisher_name, version, install_location, uninstall_location;
}
List<myProgramClass> program_list = new List<myProgramClass>();
myProgramClass new_entry = new myProgramClass() { name = "Name", publisher_name = "pub Name", version = "1.02", install_location = "dir", uninstall_location = "" };
program_list.Add(new_entry);
program_list = program_list.Where(s => string.IsNullOrWhiteSpace(s.name)).Distinct().ToList();

Which way is better to map a string array and null check? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What is the best way to map the result of adds to Codes?
result.adds contains a string array:
string[] adds = new string[] { "A", "B", "C" };
Option 1:
var car = new Car()
{
Name = request.Name,
Codes = (result.adds != null) ? adds : new string[] { }
};
In my opinion option 1 seems strange with creating a new empty string array when the result is false. I also have my doubts about mapping within the object mapping itself. On the other hand, it's direct and fast.
Option 2:
if (result.adds != null)
{
adds = results.adds;
}
var car = new Car()
{
Name = request.Name,
Codes = adds
};
Option 2 null check seems overkill to define the code seperately. It could simply be included in the mapper.
Of the two options you've presented option #1 looks the cleanest. However, you can simplify it more if you can use the null coalescing operator:
var car = new Car()
{
Name = request.Name,
Codes = result.adds ?? Array.Empty<string>()
};
This will use an empty string array in results.add is null.

check whether a List<string> contains an element in another List<string> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a list with sentences. I have another List with particular words. I want the sentences from the list, if the sentence have at least one words from the list below. That sentence should be selected and stored in a variable.
List<string> features = new List<string>(new string[] { "battery", "screen", "audio" });
Linq Any with Contains should to this
List<string> features = new List<string>(){ "battery", "screen", "audio" };
List<string> sentences = new List<string>() { "this is a new screen", "i need a new battery", "there is no foo in my bar" };
List<string> result = sentences.Where(x => features.Any(y => x.Contains(y))).ToList();
you can use linq.
List<string> features = new List<string>(new string[] { "battery", "screen", "audio" });
// or simpler
//List<string> features = new List<string>{ "battery", "screen", "audio" };
List<string> listOfLines = GetAllLinesHereInList();
var filteredLines = listOfLines.Where(ln=>features.Any(f=>ln.IndexOf(f) > -1));

Categories