Jak skopiować elementy z listy do listy bez foreach?

Jak przenieść elementy zawarte w jednym List do drugiego w C# bez użycia foreach?

Author: yoozer8, 2009-12-23

7 answers

Możesz spróbować tego:

List<Int32> copy = new List<Int32>(original);

Lub jeśli używasz C # 3 i. NET 3.5, z Linq, możesz to zrobić:

List<Int32> copy = original.ToList();
 484
Author: Lasse Vågsæther Karlsen,
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-12-23 11:19:08

Aby dodać zawartość jednej listy do innej listy, która już istnieje, możesz użyć:

targetList.AddRange(sourceList);

Jeśli chcesz utworzyć nową kopię listy, Zobacz odpowiedź Lasse ' a.

 157
Author: Jon Skeet,
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-12-23 11:20:06

Dla listy elementów

List<string> lstTest = new List<string>();

lstTest.Add("test1");
lstTest.Add("test2");
lstTest.Add("test3");
lstTest.Add("test4");
lstTest.Add("test5");
lstTest.Add("test6");

Jeśli chcesz skopiować wszystkie elementy

List<string> lstNew = new List<string>();
lstNew.AddRange(lstTest);

Jeśli chcesz skopiować pierwsze 3 elementy

List<string> lstNew = lstTest.GetRange(0, 3);
 30
Author: Paras,
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
2018-01-03 15:41:16

I jest tak, jeśli potrzebne jest skopiowanie pojedynczej właściwości do innej listy:

targetList.AddRange(sourceList.Select(i => i.NeededProperty));
 5
Author: usefulBee,
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
2016-08-25 17:06:04

Ta metoda utworzy kopię Twojej listy, ale twój typ powinien być serializowalny.

Użycie:

List<Student> lstStudent = db.Students.Where(s => s.DOB < DateTime.Now).ToList().CopyList(); 

Metoda:

public static List<T> CopyList<T>(this List<T> lst)
    {
        List<T> lstCopy = new List<T>();
        foreach (var item in lst)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, item);
                stream.Position = 0;
                lstCopy.Add((T)formatter.Deserialize(stream));
            }
        }
        return lstCopy;
    }
 4
Author: garish,
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
2015-04-03 11:15:17

OK to działa dobrze Z powyższych sugestii GetRange( ) nie działa u mnie z listą jako argument...so słodzenie trochę z postów powyżej: (dzięki wszystkim:)

/*  Where __strBuf is a string list used as a dumping ground for data  */
public List < string > pullStrLst( )
{
    List < string > lst;

    lst = __strBuf.GetRange( 0, __strBuf.Count );     

    __strBuf.Clear( );

    return( lst );
}
 0
Author: Jim Stevens,
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-08-30 14:42:28

Tutaj inna metoda, ale jest trochę gorzej w porównaniu do innych.

List<int> i=original.Take(original.count).ToList();
 -7
Author: ratty,
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
2013-03-30 14:07:20