I have a LinkedList like this:
LinkedList<KeyValuePair<decimal, decimal>> qCompras = new LinkedList<KeyValuePair<decimal, decimal>>();
I want to obtain the value from the last object in the LinkedList, but I want to know the first of the two decimal.
How can I do this?
Thanks in advance!
Regards
Try this:
var result = qCompras.Last.Value.Key;
Why not use the LinkedList.Last Property property of the LinkedList?
For example:
var lastItem = qCompras.Last;
In there, you have the Key and Value properties.
Note that this is a LinkedListNode<T> and you need to use the LinkedListNode<T>.Value property get the underlying instance of T.
Related
I have a dictionary of type which contains an array of objects which have two decimal property values. For clarity it looks like this:
How do I access the values of those Easting and Northing values ?
var coordinates =(Coordinates[])values["Coordinates"];
Console.WriteLine(coordinates[0].Easting);
Console.WriteLine(coordinates[0].Northing);
To get just two values:
var easting = values["Coordinates"][0].Easting;
var northing = values["Coordinates"][0].Northing;
Explanation: since values is a dictionary:
values["Coordinates"] - get value (i.e. array) of "Coordinates" key
values["Coordinates"][0] - get 1st item of the array of "Coordinates" key
values["Coordinates"][0].Easting - get Easting property of ...
Let values be the Dictonary and the "Coordinates" is a Key inside it. So that we can access the associated values with this key by using values["Coordinates"]. In your case the value will be a collection(Array). So To access those values you need to Specify its index or you can iterate through the collection to get it's values.
As you already said it was a Dictonary<string,Object> You need to cast the object to get the Business object. If so you can use The following snippet:
var currentEasting = (Coordinates[])(values["Coordinates"][0]).Easting;
If the collection is defined like Dictonary<string,Coordinates> then you need not to cast. Can access it directly like this:
var currentEasting = values["Coordinates"][0].Easting;
You can also iterate through those Values; this code will help you to do that:
foreach (Coordinates Co in values["Coordinates"])
{
// access each values
var someEasting = Co.Easting
}
It is a bit unclear how the dictionary is defined. If it is defined as Dictionary<string, object>, you will either have to use reflection to get data from the value, or you will have to do a hard-coded cast:
var coords = (Coordinates[])values["Coordinates"];
var firstEast = coords[0].Easting;
This will of course fail if the object is not of type Coordinates.
If the dictionary is defined as Dictionary<string, Coordinates[]>, then it is simple:
var firstEast = values["Coordinates"][0].Easting;
I've an IEnumerable list named list. It keeps these sample values:
I want to access and assign to any variables these Count, Start and End values, whenever I want. How can I do this?
The IEnumerable itself doesn't have Count, Start, or End. It's elements do, so you'll need to identify the element in the collection from which you want to read those values. For example, to read the values on the first element:
var firstCount = list.First().Count;
var firstStart = list.First().Start;
var firstEnd = list.First().End;
Or if you want to get a collection of all the Count values, something like this:
var allCounts = list.Select(c => c.Count);
You're operating on a collection of elements, not a single element. So to get information from any particular element you first need to identify it from the collection. And there are lots of methods you can chain together to identify any given element or set of elements.
Use a loop ?
foreach(var item in list)
{
var count = item.Count;
}
Or use ToList and convert it to List<T> then you can access your value with index:
var myList = list.ToList();
var count = myList[0].Count;
Also if you know the type you can cast your IEnumerable to IList<T> in order to use indexer.
It's possible to access the list with Linq using the namespace
using System.Linq;
like so
var firstListElement = list.ElementAt(0);
var firstListElementCount = firstListElement.Count;
// do more stuff with my first element of the list
Try:
list.Count()
list.First() or list.FirstOrDefault()
list.Last() or list.LastOrDefault()
I have such code:
List<Vector3> list = fillTheList();
How can I get element from it on specyfied position?
You can get it by using its indexer.
var item = list[index];
Lists also have indexers like arrays do.
For example if you want to have the second item in the list you would write:
var vectorFromList = list[1]
List has an indexer that accepts integer, the index of the element in the List
ls[0] // gets the first element
ls[1] // gets the second element
There is a SortedList
slLanguage = new SortedList();
slLanguage.Add("Bahasa","id-ID");
slLanguage.Add("Chinese Simplified(中文简体)","zh-CN");
slLanguage.Add("Chinese Traditional(中文繁體)","zh-TW");
slLanguage.Add("Kazakh","kk-KZ");
slLanguage.Add("Russian(русский)","ru-RU");
slLanguage.Add("Vietnamese(Việt)","vi-VN");
slLanguage.Add("English", "en-US");
How can I get the key by value?
For example: Get the item key "zh-CN"
If you would like to get the key from a value, you may use SortedList.IndexOfValue(object value) to get the index of the value you specify. Then, use SortedList.GetKey(int index) to return a key as object from the value's index we just gathered.
Example
SortedList slLanguage = new SortedList(); //Initializes a new SortedList of name slLanguage
//Add the keys and their values to the list
slLanguage.Add("Bahasa", "id-ID");
slLanguage.Add("Chinese Simplified(中文简体)", "zh-CN");
slLanguage.Add("Chinese Traditional(中文繁體)", "zh-TW");
slLanguage.Add("Kazakh", "kk-KZ");
slLanguage.Add("Russian(русский)", "ru-RU");
slLanguage.Add("Vietnamese(Việt)", "vi-VN");
slLanguage.Add("English", "en-US");
//
object returnedKey = slLanguage.GetKey(slLanguage.IndexOfValue("zh-CN")); //Gets the key from zh-CN as returnedKey of type object
Thanks,
I hope you find this helpful :)
There's probably a better way to do this, but here's one way to do it:
int index = slLanguage.IndexOfValue("zh-CN");
var item = slLanguage.GetKey(index);
Looking the key from Value would be not efficient and defeats the purpose of sorted List. Sorted list is really a sorted Dictionary named confusingly as SortedList.
This is my generic list. I would like to return the object CompanyEmail by finding the object based on the index. How do I do this ?
List<CompanyEmail> companyEmail = (List<CompanyEmail>)ViewState["companyEmail"];
try:
List<Data> data = new List<Data>();
Data temp = data[1];
in your list:
companyEmail[index];
why on index, you may use the Dictionary Class (it's generic too). http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
hope I got you right.
This should do the trick:
http://msdn.microsoft.com/en-us/library/x1xzf2ca.aspx
Use:
List<T>.FindIndex(Predicate)