Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to add all entries from <code>AftList2</code> to <code>lstAux</code> you should define <code>AftList2</code> as IEnumerable&lt;> with elements of type <code>DetalleCita</code> (being <code>IEnumerable&lt;DetalleCita&gt;</code> is enough to be used as parameter of <code>AddRange()</code> on <code>List&lt;DetalleCita&gt;</code>). For example like this:</p> <pre><code>var AftList2 = GridData2.Values.Where(r =&gt; r != null).Cast&lt;DetalleCita&gt;(); </code></pre> <p>And then you can add all its elements to <code>lstAux</code>:</p> <pre><code>lstAux.AddRange(AftList2); </code></pre> <p>Clarification:</p> <p>I think you are misunderstanding what extension method <code>ToList()</code> does. It <strong>creates</strong> new list from <code>IEnumerable&lt;T&gt;</code> and its result is not connected with original <code>IEnumerable&lt;T&gt;</code> that it is applied to.</p> <p>That is why you are just do nothing useful trying to do <code>list.ToList().AddRange(...)</code> - you are copying list to (another newly created by <code>ToList()</code>) list, update it and then basically throwing away it (because you are not even doing something like <code>list2 = var1.ToList()</code>, original <code>var1</code> stays unchanged after that!!! you most likely want to save result of <code>ToList()</code> if you are calling it).</p> <p>Also you don't usually need to convert one list to another list, <code>ToList()</code> is useful when you need list (<code>List&lt;T&gt;</code>) but have <code>IEnumerable&lt;T&gt;</code> (that is not indexable and you may need fast access by index, or lazy evaluates but you need all results calculated at this time -- both situations may arise while trying to use result of LINQ to objects query for example: <code>IEnumerable&lt;int&gt; ints = from i in anotherInts where i &gt; 20 select i;</code> -- even if <code>anotherInts</code> was <code>List&lt;int&gt;</code> result of query <code>ints</code> <strong>cannot be cast</strong> to <code>List&lt;int&gt;</code> because it is not list but implementation of <code>IEnumerable&lt;int&gt;</code>. In this case you could use <code>ToList()</code> to get list anyway: <code>List&lt;int&gt; ints = (from i in anotherInts where i &gt; 20 select i).ToList();</code>).</p> <p>UPDATE:</p> <p>If you really mean union semantics (e.g. for { 1, 2 } and { 1, 3 } <em>union</em> would be something like { 1, 2, 3 }, with no duplication of equal elements from two collections) consider switching to <code>HashSet&lt;T&gt;</code> (it most likely available in your situation 'cause you are using C# 3.0 and I suppose yoou have recent .NET framework) or use <code>Union()</code> extension method instead of <code>AddRange</code> (I don't think this is better than first solution and be careful because it works more like <code>ToList()</code> -- <code>a.Union(b)</code> return new collection and does NOT updates either <code>a</code> or <code>b</code>).</p>
 

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