Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a generic list to ISet

Tags:

Has anyone had to assign a list to an ISet? How do I go about and do this?

Say I have the class

class Foo
{
  ISet<Item> Items { get; set; }
}

Now, I want to do the following:

var foo = new Foo() { Items = new List<Item>() { new Item() } }
like image 602
Erion Avatar asked Apr 27 '11 17:04

Erion


People also ask

How do you convert a generic list?

Select(x => new TestClass() { AAA = (string)x[0], BBB = (string)x[1], CCC = (string)x[2] }). ToList();

What is iset in c#?

This interface provides methods for implementing sets, which are collections that have unique elements and specific operations. The HashSet<T> and SortedSet<T> collections implement this interface.

Is it possible to implement Iset<T> in Java?

So, this isn't possible. The only classes that implement ISet<T> are HashSet<T> and SortedSet<T>. Thanks for contributing an answer to Stack Overflow!

How to convert list to set in Python?

The simplest way to convert list to set in Python is by using the set () function. The set () method is used to convert an iterable element such as a list, dictionary, or tuple into the set. In this method, we use for loop to access each element of a list and add that element in a set by using add () function.

How do I load a data set of entity types?

We start the load process with the first data table in the data set: Inspect the provided entity type and get a list of all its properties. For every property, check if we can map the property to a specified column in the current data table. This is called a column property map. Create a new list of the entity type.

How do I create an instance of one type from another?

Write "copy" or "clone" method creating instance of one type out of the instance of another. Clone elements one by one and add to a new list. Thanks a lot. You are very welcome. Good luck, call again. Please Sign up or sign in to vote. There are libraries that make this kind of thing easier like autopmapper.


2 Answers

List<Item> myList = ...
foo.Items = new HashSet<Item>( myList );

Keep in mind that a Set, unlike a List, must contain every element exactly once. Therefore, if myList contains multiple copies of some elements, all those copies, except one, will not make it into the set.

Equality of elements (for detecting multiple copies) is defined by the Equals and GetHashCode methods. If you would like to use a different definition of equality, you can use the overload of HashSet constructor that takes an IEqualityComparer<Item>.

like image 156
Fyodor Soikin Avatar answered Sep 26 '22 00:09

Fyodor Soikin


List<T> doesn't implement the ISet<T> interface… So, this isn't possible.

The only classes that implement ISet<T> are HashSet<T> and SortedSet<T>.

The closest you could get would be (if you cut out the unnecessary List object in between):

var foo = new Foo { Items = new HashSet<Item> { new Item() } };
like image 39
Justin Niessner Avatar answered Sep 26 '22 00:09

Justin Niessner