collections

Collections Question (.NET 3.5, C#)

I'm stuck and I need help. Anyway I have Visual Studio 2008 console program that needs to log a Date, String, Boolean, and an integer for each round of a game. Say the user plays 10 rounds I would have 10 of each. I want to save those records in a data structure (collection i believe) and then after the game is finished loop through th...

Implementing IList interface

I am new to generics. I want to implement my own collection by deriving it from IList<T> interface. Can you please provide me some link to a class that implements IList<T> interface or provide me a code that at least implements Add and Remove methods? ...

Isn't a generic IList assignable from a generic List?

bool IsTypeAGenericList(Type listType) { typeof(IList<>).IsAssignableFrom(listType.GetGenericTypeDefinition()) } returns false when given typeof(List<int>). I assume this is because the two type parameters can be different, correct? ...

Is there a more elegant way of adding an item to a Dictionary<> safely?

I need to add key/object pairs to a dictionary, but I of course need to first check if the key already exists otherwise I get a "key already exists in dictionary" error. The code below solves this but is clunky. What is a more elegant way of doing this without making a string helper method like this? using System; using System.Collecti...

How to cast a complete list without iterating through it in Java

Say I have this class : public class BaseJob{ String name; public void setName(String name){ this.name=name; } public String getName() { return name; } } and another class that extends it : public class DetailedJob extends BaseJob{ public void doThing(); } Furthermore, I have this method in another class : List<BaseJob> ...

Check if a value is in a collection with LINQ

Hello, I have a class "Employee", this has an IList<> of "TypeOfWork". public class Employee { public virtual IList<TypeOfWork> TypeOfWorks { get; set; } } public class TypeOfWork { public virtual Customer Customer { get; set; } public virtual Guid Id { get; set; } public virtual string Name{ get; set; } public vi...

Make blank params[] nil

When a user submits a form and leaves certain fields blank, they get saved as blank in the DB. I would like to iterate through the params[:user] collection (for example) and if a field is blank, set it to nil before updating attributes. I can't figure out how to do this though as the only way I know to iterate creates new objects: col...

Multivariable Partial in Ruby on Rails

I have a partial that I want rendered with a collection and another variable. Is it possible to pass more than one variable to a partial? To illustrate: Category HABTM Brands This is just semi-pseudo-code, but I want to do something like: <% @categories.each do |c| %> <%= c.name %> <%= render :partial => "mypartial", :collec...

Is there a Dictionary<string, object> collection which allows multiple keys?

I currently have a menu with subitems that is being stored in this dictionary variable: private Dictionary<string, UserControl> _leftSubMenuItems = new Dictionary<string, UserControl>(); So I add views to the e.g. the "Customer" section like this: _leftSubMenuItems.Add("customers", container.Resolve<EditCustomer>()); _leftSubMen...

ITypedList alternative?

I have a helper class that implements ITypedList, to provide objects for databinding against custom collections. My implementation allows me to easily specify that I want sub-properties of objects to be available for data binding, for instance I can bind to "Id", "Name", and also "Children.Count". Now, my problem now is that in order t...

An easy way of iterating over all unique pairs in a collection

I have a c# 3 HashSet object containing several elements. I want to check something between every pair of them, without repeating [(a,b)=(b,a)], and without pairing an element with itself. I thought about switching to some sort of List, so I can pair each element with all of his following elements. Is there an option of doing something l...

Howto merge into a database

I am having trouble figuring out the best way to approach this problem, I have two different list of items, one in the DB, one not. Both list have item ids and quantity, I need to merge the two list into the DB. My first inclination is to just pull all of the items out of the DB then merge the two list in the application logi...

In C# .NET, what is the reason for array.Length vs. list.Count?

Possible Duplicate: count vs length vs size in a collection Am I overlooking a semantic difference between "Length" and "Count", or did perhaps some implementation detail in the .NET Framework require different names for these similar concepts? Given the close attention that was paid to naming and everything else in the framewor...

SubSonic 3 Compatiblity Problem with Not Autogenerated Collections?

Had a system with v2 running well. Swapped out the Visual Studio reference for the v2 DLL to the v3 DLL. Rebuilt the project. Errors. I used to have things called "MyYahooCollection", now I only have "MyYahoo" and "MyYahooTable". The collections seem to be gone. What did I miss? I am using SQL Server 2005 on the back-end and am imple...

Which collection(s) to store groups of users?

I want to make a collection that stores groups of users. A user can be a member of multiple groups, groups can be nested, a selection of groups should return a distinct IEnumerable<user> . I could write this from scratch of course using the standard List<T>,Dictionary<T,U> etc collections, but is there a built-in collection that already...

How can I create unit tests involving collections in c#?

I have a number of methods in C# which return various collections which I wish to test. I would like to use as few test APIs as possible - performance is not important. A typical example is: HashSet<string> actualSet = MyCreateSet(); string[] expectedArray = new string[]{"a", "c", "b"}; MyAssertAreEqual(expectedArray, actualSet); //.....

Sorting a collection of objects

If I have a simple list of Strings: List<String> stringList = new ArrayList<String>(); I can sort it with: Collections.sort(stringList); But suppose I have a Person class: public class Person { private String name; private Integer age; private String country; } And a list of it: List<Person> personList = new ArrayList<...

An array or Generic collection?

I need to pass a list of Days (number and name) to an view! Whats the best way to do this? I was thinking of creating a generic collection, but not sure how? or an array? ...

Scala best way of turning a Collection into a Map-by-key? (2nd variant)

(This is a variant to this Q&A) Say I have this: List( "foo", "bar", "spam" ) I want to create a Map for which the key is the length of the String and the value is a Collection of all the Strings that have that length. In other words, given the about List, we'd get: Map( 3 -> List(foo, bar), 4 -> List(spam) ) The code I've written...

How to prevent a method caller from modifying a returned collection?

I have methods returning private collections to the caller and I want to prevent the caller from modifying the returned collections. private readonly Foo[] foos; public IEnumerable<Foo> GetFoos() { return this.foos; } At the moment the private collection is a fixed array, but in the future the collection might become a list if th...