IEnumerable Select statement with ternary operator

Simon Linder

I have an odd behavior using an IEnumerable<string> with a ternary operator and a Select statement.
I have two lists with different objects. One list contains Enums the other list contains objects. Those objects do have a String property.
If one list is null or empty I want to get the values of the other list.
Here is some code:

public class ExportItem
{
    public string Type;
    ...
}

public enum ExportType
{
    ExportType1,
    ExportType2,
    ...
}

The List<ExportItem> is always filled by a config file. The List<ExportType> is filled if command line arguments are provided. So if List<ExportType> is filled I want to use them, otherwise I want to use those from the config file.
So my code ist like this:

IEnumerable<string> exportTypes = MyListOfExportTypes != null &&
    MyListOfExportTypes.Any() ? MyListOfExportTypes.Select(x => x.ToString()) :
    MyListOfExportItems.Select(x => x.Type);

The thing is that exportTypes is null but I don't get it...
When I do this with if-else everything works as expected. Also if exportTypes is of type List<string> and I call ToList() after the Select statement everything works fine.
Using var a = MyListOfExportTypes.Select(x => x.ToString()); and var b = MyListOfExportItems.Select(x => x.Type); does work as expected.
Must be something with the ternary operator and/or IEnumerable. But what?

Or what do I miss? Any suggestions?

EDIT:
I now have a screenshot... enter image description here

Note that the code above foreach works nevertheless...

Y.S

Not sure if this was answered, But I think that this is related to the fact that you are using LINQ deferred execution.

When writing LINQ queries, there is a difference between creating the query and executing it.

Writing the select statement, is creating the query, adding ToList() executes it. Think of it like writing SQL query in SQL server console (that's the writing stage), and once you hit F5 (Or the play button) you execute it.

I hope this little code sample will help to clarify it.

    public class SomeClass
    {
        public int X { get; set; }
        public int Y { get; set; }

        public void Test()
        {
            //Here I'm creating a List of Some class
            var someClassItems = new List<SomeClass> { 
                new SomeClass { X = 1, Y = 1 }, 
                new SomeClass { X = 2, Y = 2 } 
            };

            //Here I'm creating a Query
            //BUT, I'm not executing it, so the query variable, is represented by the IEnumerable object
            //and refers to an in memory query
            var query = someClassItems.
                Select(o => o.X);

            //Only once the below code is reached, the query is executed.
            //Put a breakpoint in the query, click the mouse cursor inside the select parenthesis and hit F9
            //You'll see the breakpoint is hit after this line.
            var result = query.
                ToList();

        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IEnumerable Select statement with ternary operator

From Dev

Ternary operator in select statement

From Dev

Ternary operator is not a statement

From Dev

Ternary Operator in jQuery statement

From Dev

Convert if statement to ternary operator

From Dev

SQL statement equivalent to ternary operator

From Dev

Ternary operator inside a return statement

From Dev

Max Ternary operator IF statement compare

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

From Dev

Question Regarding Using Ternary Operator in Switch Statement

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

From Dev

Cannot embed ternary operator into echo statement

From Dev

PHP inline statement using ternary logic operator "?:"

From Dev

ternary operator causes an error inside if statement

From Dev

Can we use ternary operator within an if statement?

From Dev

MySql SELECT with if statement and LIKE operator

From Dev

Select statement using 'EXISTS' and 'IN' operator

From Dev

Ternary operator evaluating conditional statement while condition not met

From Dev

Difference between ternary (conditional) operator and if statement returning an Action

From Dev

Ternary Operator (in-line "if statement") conversion from MySQL to MySQLi?

From Dev

Java Ternary operator outputs different result than if else statement

From Dev

(How) is it possible to catch an exception in a Java ternary operator statement?

From Dev

How would I write this if else statement as ternary operator

From Dev

How can I convert this if else statement into ternary operator?

From Dev

Undefined variable when converting a ternary operator into traditional if statement

From Dev

+ operator in a ternary operator

From Dev

Ruby ternary operator (or) or operator

Related Related

  1. 1

    IEnumerable Select statement with ternary operator

  2. 2

    Ternary operator in select statement

  3. 3

    Ternary operator is not a statement

  4. 4

    Ternary Operator in jQuery statement

  5. 5

    Convert if statement to ternary operator

  6. 6

    SQL statement equivalent to ternary operator

  7. 7

    Ternary operator inside a return statement

  8. 8

    Max Ternary operator IF statement compare

  9. 9

    convert some lines of php statement into ternary operator

  10. 10

    How to use AND in if statement using ternary operator

  11. 11

    Question Regarding Using Ternary Operator in Switch Statement

  12. 12

    convert some lines of php statement into ternary operator

  13. 13

    How to use AND in if statement using ternary operator

  14. 14

    Cannot embed ternary operator into echo statement

  15. 15

    PHP inline statement using ternary logic operator "?:"

  16. 16

    ternary operator causes an error inside if statement

  17. 17

    Can we use ternary operator within an if statement?

  18. 18

    MySql SELECT with if statement and LIKE operator

  19. 19

    Select statement using 'EXISTS' and 'IN' operator

  20. 20

    Ternary operator evaluating conditional statement while condition not met

  21. 21

    Difference between ternary (conditional) operator and if statement returning an Action

  22. 22

    Ternary Operator (in-line "if statement") conversion from MySQL to MySQLi?

  23. 23

    Java Ternary operator outputs different result than if else statement

  24. 24

    (How) is it possible to catch an exception in a Java ternary operator statement?

  25. 25

    How would I write this if else statement as ternary operator

  26. 26

    How can I convert this if else statement into ternary operator?

  27. 27

    Undefined variable when converting a ternary operator into traditional if statement

  28. 28

    + operator in a ternary operator

  29. 29

    Ruby ternary operator (or) or operator

HotTag

Archive