1

I have this very simple Linq query (the .Dump() function is being used with LinqPad in order to view the results):

void Main()
{
    var myList = new List<string>
    {
        "One", "Two", "Three"
    };


    var result = from o in myList
    select o;

    result.Dump();
}

How can I inject an if statement into the Linq query that populates result so that it checks to see if "Four" is in the list--and if it is not, then add it? I realize that there are ways to do this by looking at result after Linq has populated result, but I would like to add this logic into a single Linq query.

Thank you.

EDIT/CLARIFICATION: The "if" logic needs to be inside the Linq query. Here is the skeletal code:

 var result = from o in myList
    select o
    [now add "four" if and only if it is not in the result]
    [now end the query and ensure `result` contains "one", "two", "three", and "four"];

SECOND EDIT: I am trying to query and add to the list in a single operation. By the way, I made this very simple example instead of giving the much more complex scenario I'm trying to handle, by the way, so it's not as if I am struggling with how to add a new element to a List object. :)

So here is what I was going to do:

// query the list to see if "four" is in the list (a Linq query goes here to check for "four")

// if the result of the above is false then add "four" to the myArray List (add the new array element here)

Instead, I would like to do the following:

// query the list for its contents and also add "four" if it is not in the list. // all this needs to be done in a single "select" using Linq

Is this not possible?

Thank you.

2
  • 1
    What makes you think that the list will be queried twice? LINQ expressions are declarative not execution logic. Commented Nov 8, 2016 at 1:57
  • Revised after your comment. Actually, while making this easy example, I think I am creating confusion. My "more complex" real Linq issue would iterate through the list twice if it has nested selects. I don't want to go into that complexity in this question, however, so I revised my OP. Thanks. Commented Nov 8, 2016 at 1:59

2 Answers 2

3

You can use Union if you switch to method syntax:

var result = myList.Union(new [] {"Four"});

There's not really a clean way to to it using just query syntax. Query syntax only supports a small set of Linq operators, of which union is not one.

Sign up to request clarification or add additional context in comments.

9 Comments

@Jazimov Union does that by definition. It's the "union" set of the two collections, which by definition does not include items in both lists twice.
There's not an if operator if that's what you're looking for. I suppose you could do .Concat(new [] {"four"}).Where(s => !myList.Contains(s)) but that's effectively the same as Union.
Union will not query either list twice. It will keep track of what items have already bee found so it doesn't return the same element twice.
Note that Where(!Contains) will enumerate myList again, so if that is your goal then Where is not the right answer.
Note: Union removes all duplicates, which has not been discussed.
|
3

You could create a second list of items that you want to conditionally add, then combine the Except and Concat methods:

var myList = new List<string>
{
    "One", "Two", "Three"
};

var others = new List<string> {
    "Four"
};

var result = myList.Concat(others.Except(myList));

result.Dump();

5 Comments

Please see clarification in question. I need the conditional logic to be inside a single Linq construct. Thanks.
@Jazimov Do you mean that you want to use the query syntax? Or use only one method call?
I gave a skeletal example. I suppose I would answer that I want to use the query syntax vs one method call. Thanks.
I might have missed your .Except() above. If so, apologies. I think, however, that Union might be a more direct answer (see answer below this one). But thanks for illustrating .Except, it will be useful to me.
You could also flip it: myList.Except(others).Concat(others) but this wouldn't preserve the order of the original list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.