Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I only recive one Object in this foreach-statement?
    primarykey
    data
    text
    <p>I get kind of mad with this piece of code:</p> <pre><code>Entity.GuidArrayArray.gaa[0]) is a static Guid[][] Entity.Customer is an non static ClassObject with several propperties e.G .id which is a Guid Entity.DataSource.LbCustomer.CustomerSource is a static BindingList&lt;Entity.Customer&gt; Entity.CustomerCollection.customerCollection is a static List&lt;Entity.Customer&gt; </code></pre> <p>gaa[0] contains 6 different Guids which are equal to the .id of the same ammount of Customer in customerCollection. Before this Code starts CustomerSource contains Clones of customerCollection.</p> <p>The outcome i wish to get (in this special case) is (same as before) a Clone of Each Object in customerCollection added into CustomerSource. What I get instead is only the last Object in the list. I guess it has something to do with the foreach-statement, that each guid in gaa[0] selects its connected Object and overwrites the previos, but i see no way to prevent this. I hope you have any idea?</p> <pre><code> public static void GuidInput() { CustomerFiller(Entity.GuidArrayArray.gaa[0]); } private static void CustomerFiller(Guid[] guidArray) { foreach (Guid g in guidArray) Entity.DataSource.LbCustomer.CustomerSource = ToBindingList(Entity.CustomerCollection.customerCollection.Where(a =&gt; a.id == g).ToList()); } private static BindingList&lt;T&gt; ToBindingList&lt;T&gt;(List&lt;T&gt; input) { BindingList&lt;T&gt; output = new BindingList&lt;T&gt;(Clone(input)); return output; } private static List&lt;T&gt; Clone&lt;T&gt;(IEnumerable&lt;T&gt; input) { List&lt;T&gt; clonedList = new List&lt;T&gt;(input); return clonedList; } </code></pre> <p>EDIT:</p> <hr> <p>Solution:</p> <pre><code>private static void CustomerFiller(Guid[] guidArray) { foreach (Guid g in guidArray) { List&lt;Entity.Customer&gt; abc =new List&lt;Entity.Customer&gt;(); foreach (Guid g in guidArray) abc.AddRange(Entity.CustomerCollection.customerCollection.Where(a =&gt; a.id == g)); Entity.DataSource.LbCustomer.CustomerSource = ToBindingList(abc); Entity.DataSource.LbCustomer.CustomerSource = Reduce(Entity.DataSource.LbCustomer.CustomerSource, x =&gt; x.id); } </code></pre> <p>But I still hope to get rid of the foreach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload