Note that there are some explanatory texts on larger screens.

plurals
  1. POTransform sequence in Linq while being aware of each Select/SelectMany result
    primarykey
    data
    text
    <p>Here's my problem. I have one specific list, which I'll present as a <code>int[]</code> for simplicity's sake.</p> <pre><code>int[] a = {1,2,3,4,5}; </code></pre> <p>Suppose I need to transform each item on this list, but depending on the situation, I may return an int or an array of ints.</p> <p>As an example, suppose I need to return <code>{v}</code> if the value is <strong>odd</strong>, and <code>{v,v+1}</code> if the value is <strong>even</strong>. I've done this:</p> <pre><code>int[] b = a.SelectMany(v =&gt; v % 2 == 0 ? new int[] { v, v+1 } : new int[] { v }) .ToArray(); </code></pre> <p>So if I run this, I'll get the expected response:</p> <pre><code>{1,2,3,3,4,5,5} </code></pre> <p>See that I have repeating numbers, right? <code>3</code> and <code>5</code>. I don't want those repeating numbers. Now, you may tell me that I can just call <code>.Distinct()</code> after processing the array.</p> <p>This is the problem. The <code>SelectMany</code> clause is fairly complex (I just made up a simpler example), and I definitely don't want to process <code>3</code> if it's already present in the list.</p> <p>I could check if <code>3</code> is present in the original list. But if I got <code>3</code> in the <code>SelectMany</code> clause, I don't want to get it again. For instance, if I had this list:</p> <pre><code>int[] a = {1,2,3,4,5,2}; </code></pre> <p>I would get this:</p> <pre><code>{1,2,3,3,4,5,5,2,3} </code></pre> <p>Thus returning <code>v</code> (my original value) and <code>v+1</code> again at the end. Just so you can understand it better <code>v+1</code> represents some processing I want to avoid.</p> <p>Summarizing, this is what I want:</p> <ol> <li>I have a list of objects. <strong>(Check)</strong></li> <li>I need to filter them, and depending on the result, I may need to return more than one object. <strong>(Check, used <code>SelectMany</code>)</strong></li> <li>I need them to be distinct, but I can't do that at the end of the process. I should be able to return just <code>{v}</code> if <code>{v+1}</code> already exists. <strong>(Clueless...)</strong></li> </ol> <p>One thing I thought about is writing a custom <code>SelectMany</code> which may suit my needs, but I want to be sure there's no built-in way to do this.</p> <p><strong>EDIT</strong>: I believe I may have mislead you guys with my example. I know how to figure out if <code>v+1</code> is in a list. To be clear, I have one object which has 2 int properties, Id and IdParent. I need to "yield return" all the objects <strong>and</strong> their parents. But I just have the ParentId, which comes from the objects themselves. I'm able to know if <code>v+1</code> is in the list because I can check if any object there has the same Id as the ParentId I'm checking.</p> <p><strong>ANSWER</strong>: I ended up using <code>Aggregate</code>, which can be used to do exactly what I'm looking for.</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