What I should use to store a list of assosiated values to be able to retrieve, modify and save one of the elements
I am new to C# and I do not know how to do this
I have a set of associated values
dateValue (string) valueOne (decimal) valueTwo (decimal)
I need to be able to pick up the values by a certain date (I could change the dateValue to datetime) and save all the values after modify开发者_开发百科 the valueOne and valueTwo
Should I create a list of objects and loop all the list searching for the proper dateValue which valueOne and valueTwo I want to modify?
What will be the best solution Create a class, instantiate all the values and then add them to a list? How could I search for an specific date?
It sounds like a Dictionary
may be what you are looking for. A dictionary is a means of storing a Key/value pair and allowing fast and easy lookup of items by key. The key will be dateValue
. The Value of the dictionary might be a class that contains valueOne
and valueTwo
. For example:
var dictionary = new Dictionary<string, Values>();
//Add an item to the dictionary
dictionary.Add("thekey", new Values {ValueOne = 1, ValueTwo = 2});
//Get the item out of the dictionary by key
var values = dictionary["thekey"];
//Update the value of ValueOne for "thekey"
values.ValueOne = 7;
//Print the new value
Console.WriteLine(dictionary["thekey"].ValueOne);
And the class:
public class Values
{
public decimal ValueOne { get; set; }
public decimal ValueTwo { get; set; }
}
As an aside, why not storedateValue
as a DateTime rather than a string? This allows you to to have access to various APIs for working with date and time, such as formatting it for display, arithmetic, etc.
In object-oriented programming, you should represent your associated values with a class:
public class MyAssociatedValues // come up with a better name
{
public DateTime Date{get;set;}
public decimal Value1{get;set;} // needs a better name
public decimal Value2{get;set;} // ditto
}
Once you are representing your collection as an IEnumerable<MyAssociatedValues>
(this could be a List
, an array, or a number of other structures that implement IEnumerable<>
), you can easily create a Dictionary
to key these values based on their date.
var valuesByDate = values.ToDictionary(v => v.Date);
For easy retrieval by dateValue you could either use a Dictionary with a Tuple containing the two values
Dictionary<string, Tuple<string, string>>
or a Lookup, which is basically a Dictionary that can store a collection of values per key.
I would use a Dictionary to map the DateTime to a structure that contains valueOne and valueTwo.
Something like:
struct Data
{
public decimal valueOne { get; set; }
public decimal valueTwo { get; set; }
}
Dictionary<DateTime, Data> map = new Dictionary<DateTime, Data>();
void addToMap(DateTime dt, decimal one, decimal two)
{
map[dt] = new Data() { valueOne = one, valueTwo = two };
}
Firstly, I would create a class (POCO) instead of a struct unless you have a good reason for a struct.
public class TheClass
{
public DateTime TheDate { get; set; }
public decimal ValueOne { get; set; }
public decimal ValueTwo { get; set; }
}
Secondly, I would store them in any data structure that implements IEnumerable (collection-type) that makes sense. The one that doesn't seem to make sense to me is the one that everyone is suggesting (Dictionary) because it is very much possible that you have 2 objects with the same datetime, so why everyone is suggesting a dictionary baffles me. The only thing I can think of is that they are assuming you only have one pair of values per datetime.
Thirdly, use LINQ to enumerate over the sub-list based on your query.
foreach (var obj in myCollection.Where(item => item.TheDate == requestedDate))
{
//Do whatever you need to do to each enumerated item.
}
Alternatively, if you really want to use the Dictionary instead of LINQ you could create an IDictionary<DateTime, IEnumerable<TheClass>>
which means you are caching the collection as the value in the Dictionary.
foreach (var obj in myDictionary[requestedDate])
{
//Do whatever you need to do to each enumerated item.
}
精彩评论