Swobodnie konwertuj między listą a liczbą
Jak mogę przekształcić {[0] } na IEnumerable<MyObject>
, a następnie z powrotem?
Chcę to zrobić, aby uruchomić serię poleceń LINQ na liście, np. Sort()
6 answers
List<string> myList = new List<string>();
IEnumerable<string> myEnumerable = myList;
List<string> listAgain = myEnumerable.ToList();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-23 12:08:20
A List<T>
jest IEnumerable<T>
, więc tak naprawdę nie ma potrzeby 'konwertowania' a List<T>
na IEnumerable<T>
.
Ponieważ List<T>
jest IEnumerable<T>
, możesz po prostu przypisać List<T>
do zmiennej typu IEnumerable<T>
.
Odwrotnie, nie każda IEnumerable<T>
jest List<T>
, więc będziesz musiał wywołać ToList()
metodę member IEnumerable<T>
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-23 16:45:13
A List<T>
jest już IEnumerable<T>
, więc można uruchamiać instrukcje LINQ bezpośrednio na zmiennej List<T>
.
Jeśli nie widzisz metod rozszerzenia LINQ, takich jak OrderBy()
zgaduję, że to dlatego, że nie masz dyrektywy using System.Linq
w swoim pliku źródłowym.
Musisz przekonwertować wynik wyrażenia LINQ z powrotem na List<T>
jawnie, chociaż:
List<Customer> list = ...
list = list.OrderBy(customer => customer.Name).ToList()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-17 12:33:38
Na bok: zauważ, że standardowe operatory LINQ (jak w poprzednim przykładzie) nie zmieniają istniejącej listy - list.OrderBy(...).ToList()
utworzy nową listę na podstawie ponownie uporządkowanej sekwencji. Jednak dość łatwo jest stworzyć metodę rozszerzenia, która pozwala na użycie lambda z List<T>.Sort
:
static void Sort<TSource, TValue>(this List<TSource> list,
Func<TSource, TValue> selector)
{
var comparer = Comparer<TValue>.Default;
list.Sort((x,y) => comparer.Compare(selector(x), selector(y)));
}
static void SortDescending<TSource, TValue>(this List<TSource> list,
Func<TSource, TValue> selector)
{
var comparer = Comparer<TValue>.Default;
list.Sort((x,y) => comparer.Compare(selector(y), selector(x)));
}
Wtedy możesz użyć:
list.Sort(x=>x.SomeProp); // etc
To aktualizuje istniejącą listę w taki sam sposób, jak zwykle robi to List<T>.Sort
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-23 13:14:32
Aby zapobiec powielaniu w pamięci, resharper sugeruje to:
List<string> myList = new List<string>();
IEnumerable<string> myEnumerable = myList;
List<string> listAgain = myList as List<string>() ?? myEnumerable.ToList();
.Tolist () zwraca nową listę niezmienną. Tak więc zmiany w listAgain nie wpływają na myList w odpowiedzi @Tamas Czinege. Jest to poprawne w większości przypadków z co najmniej dwóch powodów: pomaga to zapobiegać zmianom w jednym obszarze wpływającym na drugi obszar( luźne sprzężenie) i jest bardzo czytelne, ponieważ nie powinniśmy projektować kodu z problemami kompilatora.
Ale są pewne przypadki, jak bycie w ciasnym pętli lub pracy na wbudowanym lub niskim systemie pamięci, gdzie należy wziąć pod uwagę względy kompilatora.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-28 10:20:41
List (T) implementuje klasę IEnumerable(T) (i wiele innych, takich jak IList(T), ICollection(T), IEnumerable(T)) dlatego nie ma potrzeby konwertowania listy z powrotem do IEnumerable.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
Person person1 = new Person() { Id = 1, Name = "Person 1" };
Person person2 = new Person() { Id = 2, Name = "Person 2" };
Person person3 = new Person() { Id = 3, Name = "Person 3" };
List<Person> people = new List<Person>() { person1, person2, person3 };
//Converting to an IEnumerable
IEnumerable<Person> IEnumerableList = people;
Ale konwersja z IEnumerable Do List jest prawidłowym scenariuszem
IEnumerable<Person> OriginallyIEnumerable = new List<Person>() { person1, person2 };
List<Person> convertToList = OriginallyIEnumerable.ToList();
Jest to przydatne w Entity Framework .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:17:34