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)
Related
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.
So i am currently creating a program that maps a String key and a ArrayList of objects (currently integers)
Now i have created a class called Operator that has a method which returns a Dictionary.
To test the output of my Dictionary i decided to make pop (Messagebox) that displays the integer value.
However this proved to be a deal breaker for me. After abit of research i managed to assemble the following code:
Operator op = new Operator();
ArrayList a;
op.startCollecting().TryGetValue("Henvendelser", out a);
int value = (int) a[0];
MessageBox.Show("" + value);
Note the startCollecting method returns the Dictionary
When trying to get the data i first have to create a new empty ArrayList, clone my existing ArrayList into that ArrayList, create an integer value, pull the item from the ArrayList at index 0 and then show the messagebox.
I am originally a java programmer, and in Java i would have been able to do this:
int value = op.startCollecting().get("Henvendelser").get(0);
Am i doing it wrong in C#? is there an easier way of Retrieving data?
First things first: You should use generic lists instead of ArrayList in c#.
To your question:
Dictonary<string, List<int>> dictonary = op.startCollecting();
if(dictonary.ContainsKey("Henvendelser"))
{
List<int> list = dictonary["Henvendelser"];
int value = list[0];
MessageBox.Show(value.ToString());
}
You can retrieve your value like this :
var value = op.startCollection()["Henvendelser"];
Assuming your StartCollecting method roughly looks like this...
class Operator
{
public IDictionary<string, IList<int>> StartCollecting()
{
...
}
}
... you can use the indexers of IDictionary and IList to retrieve your value:
op.StartCollecting()["Henvendelser"][0]
I want a List of OtherObjects all belonging to certain Objects.
Is it possible to create a List like so?
List<Object, List<OtherObject>>
Or should I create a new class and do?
List<NewClass>
Or should I do something else overall? The size is dynamic so I didn't want to use an array.
How can I achieve this?
You want a Dictionary<>, e.g.
Dictionary<Object, List<OtherObject>>
Where Object is the Key and List<OtherObject> is the Value
You can use Tuple<Object, List<OtherObject>> to achieve what you want.
Tuples is for composition of properties when you don't want to create special class. You can use Tuple<Object, AnotherObject, AnotherAnotherObject> if you need.
Your code will be something like:
List<Tuple<Object, List<OtherObject>>> list;
and work with it:
foreach(var tuple in list)
{
var object = tuple.Item1;
var innerList = tuple.Item2;
}
try Using
Dictionary<Object, List<OtherObject>>
Hope i helped
Hey all is there a collection type like arrayList which i can add an object to using an ID?
effectively as the title of my post sugests a Direct object collection. so for example:
DirectCollection.addAt(23, someobject);
and
DirectCollection.getAt(23);
etc etc
i know arraylist is usable in that case but i have to generate the initial entry with a null reference object and if if the object has an ID like 23 i have to generate 22 other entries just to add it which is clearly impractical.
basically using the object position value as a unique ID.
Any thoughts?
Many thanks.
You could use Dictionary<int, YourType>
Like this:
var p = new Dictionary<int, YourType>();
p.Add(23, your_object);
YourType object_you_just_added = p[23];
Use a dictionary.
Dictionary<int, YourType>
It allows you to add/get/remove items with a given key and non continuous ranges.
You could use a Dictionary
The code for your example would be very simple:
Dictionary<int, AType> directCollection = new Dictionary<int, AType>();
directCollection.Add(23, someObjectOfAType);
AType anObject = directCollection[23];
I think KeyedCollection or Dictionary is what you need.
Use System.Collections.Hashtable. It allow to store the heterogeneous type of object (a Hashtable can hold the multiple type of object).
Example:
System.Collections.Hashtable keyObjectMap = new System.Collections.Hashtable();
//Add into Hashtable
keyObjectMap["Key_1"] = "First String";
keyObjectMap["Key_2"] = "Second String";
//Add the value type
keyObjectMap["Key_3"] = 1;
keyObjectMap["Key_4"] = new object();
//Get value/object from Hashtable
string value = (string)keyObjectMap["Key_2"];
int intValue = (int)keyObjectMap["Key_3"];
C# Array, How to make data in an array distinct from each other?
For example
string[] a = {"a","b","a","c","b","b","c","a"};
how to get
string[]b = {"a","b","c"}
Easiest way is the LINQ Distinct() command :
var b = a.Distinct().ToArray();
You might want to consider using a Set instead of an array. Sets can't contain duplicates so adding the second "a" would have no effect. That way your collection of characters will always contain no duplicates and you won't have to do any post processing on it.
var list = new HashSet<string> { };
list.Add("a");
list.Add("a");
var countItems = list.Count(); //in this case countItems=1
An array, which you start with, is IEnumerable<T>. IEnumerable<T> has a Distinct() method which can be used to manipulate the list into its distinct values
var distinctList = list.Distinct();
Finally,IEnumerable<T> has a ToArray() method:
var b = distinctList.ToArray();
I think using c# Dictionary is the better way and I can sort by value using LINQ