2

I was wondering whether It's possible to copy a list into another list using c# without performing a foreach loop and I found this link :

How do I copy items from list to list without foreach?

where Jon Skeet has posted this answear :

targetList.AddRange(sourceList);

after I visited this link a new question came to happen for me that is this possible to copy some fields of a list into some fields of another list where the schema of two lists are different without performing a foreach loop ?

Community
  • 1
  • 1
gwt
  • 2,331
  • 4
  • 37
  • 59
  • Depends if the two item types are constructible from one another. Is it really that important you don't use `foreach`? – Frédéric Hamidi Oct 08 '13 at 08:50
  • I guess you could use linq, but its still going to loop though the arrays. – string.Empty Oct 08 '13 at 08:50
  • What do you mean by "fields"? "Field" is a very specific term in C#, and I'm pretty sure you didn't mean that interpretation here. If you mean items in the list, call them items to avoid confusion. – Mels Oct 08 '13 at 08:53
  • @FredericHamidi suppose that yes ! I am worried about the perforemance , and also curious to know if there is any way – gwt Oct 08 '13 at 08:53
  • @NicolasTyler I am using EF – gwt Oct 08 '13 at 08:53
  • 2
    Any approach of this problem using LINQ will compile to a foreach loop. The fact that _you don't see the loop_, doesn't mean _there is no loop_. – Mels Oct 08 '13 at 08:54
  • @Mels By Field I mean List Item Properties – gwt Oct 08 '13 at 08:55
  • So you have two List with _different type T_? – Mels Oct 08 '13 at 08:57
  • I don't think there is a solution to this without some sort of loop. all in all you cannot improve the performance by using linq. So you should stick to the foreach and maybe try and optimize the contents of the foreach. – string.Empty Oct 08 '13 at 08:57
  • @NicolasTyler thx, but as James said in the accepted answear the code would be neater and I liked that . – gwt Oct 08 '13 at 09:05

4 Answers4

6

You can do something like:

targetlist.AddRange(sourceList.ConvertAll(x => new targetItem(){prop1 = x.prop1, prop2 = x.prop2}));

Under the scenes a loop is still happening, but the code looks neater!

James S
  • 3,558
  • 16
  • 25
2

You could use the following

Struct Foo
{
public string Bar1;
public string Bar2
}

void SomeMethod()
{
List<Input> input = new List<Input>();
List<Foo> output = new List<Foo>();

output = input.Select(a => new Foo(){Bar1 = a.bar1, Bar2 = a.bar2});
}
Steven Wood
  • 2,675
  • 3
  • 26
  • 51
0

If you as wanting to copy some specific entries from ListA into ListB you could use LINQ to retrieve the entries from ListA

`var entriesToCopy = from item in ListA
                    where item.Property == "SomeValue"
                    select item;`

Regarding your statement about schema "where the schema of two lists are different". If you are talking about a List type, these dont really have "Schemas" they are simply a collection of objects so you would either need to cast, convert, create an object of the same type in ListB from the results of entriesToCopy.

If you had a conversion specified in the objects of ListA you could use

.AddRange(entriesToCopy.Cast<ObjectTypeB>())

Stuart
  • 267
  • 1
  • 5
  • 15
0

If you know the size of the list, you are able to do something like that:

var list1 = new List<int> {1, 2, 3}
var list2 = new List<int> {0, 0, 0, 1, 1, 1} // <-- Notice different "schema"

list2[3] = list1[0]; // <--
list2[4] = list1[1]; // <--
list2[5] = list1[2]; // <-- Notice no loops

Maybe it is not the best solution, but it meets your requirements.

In any other cases, regardless of used method, you have to iterate explicitly or implicitly through the list. However you could use some of parallelization in order to make things more robust.

Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56