2

I have a bool variable _settings.Value.UsePostgreSQL and method AddConfiguration(this IServiceCollection builder, Action<DbContextOptionsBuilder> dbContextOptionsAction = null) Is it possible to use if condition in method with Action as a parameter? Something like this:

_settings.Value.UsePostgreSQL = true;
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddConfiguration(builder =>
    _settings.Value.UsePostgreSQL ?
        builder.UseSqlServer(_settings.Value.ConnectionString, options =>
            options.MigrationsAssembly(migrationsAssembly)) :
        builder.UseNpgsql(_settings.Value.ConnectionString, options =>
            options.MigrationsAssembly(migrationsAssembly)));

While I'm trying to implement this I have an error:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

developer
  • 377
  • 1
  • 3
  • 13
  • 5
    I dont even see an if statement – maccettura Aug 31 '17 at 19:06
  • Is this a theoretical question or a practical one. Please post the code that gives you an error? Of course, you can use the tertiary operator `(x) => condition(x) ? expr_A : expr_B` in a delegate. – John Alexiou Aug 31 '17 at 19:08
  • 1
    Possible duplicate of [C# Ternary Operator used in LINQ query](https://stackoverflow.com/questions/19977327/c-sharp-ternary-operator-used-in-linq-query) – Mark Schultheiss Aug 31 '17 at 19:08
  • @maccettura sorry, I'm using conditional operator instead of `if` statement, but problem is the same. – developer Aug 31 '17 at 19:09
  • https://stackoverflow.com/q/25924102/125981 might also help you here – Mark Schultheiss Aug 31 '17 at 19:16
  • You can use ternary operator in lambda only for returning value. – Fabio Aug 31 '17 at 19:16
  • 1
    "Put on hold as not clear what you're asking". Seriously? – 15ee8f99-57ff-4f92-890c-b56153 Aug 31 '17 at 19:19
  • 1
    @EdPlunkett There seems to be an influx of erroneously closed questions. You would think users would read into a question a bit before just voting to close `¯\_(ツ)_/¯` – maccettura Aug 31 '17 at 19:29
  • 1
    @EdPlunkett look at [this one](https://stackoverflow.com/questions/45969344/unable-to-remove-a-special-character-from-string) from yesterday. I am still raging about it. 5 people voted to reopen and one guy came back in and closed it again – maccettura Aug 31 '17 at 19:31
  • 1
    @maccettura I suspect a groupthink effect. I've seen a few closed as a duplicate of something only tangentially related, like somebody voted carelessly, and then four others saw "close(1)" and said "me too, let God sort 'em out!" without even reading. People (I think we've both noticed some of the same ones) get jaded and trigger happy I guess. – 15ee8f99-57ff-4f92-890c-b56153 Aug 31 '17 at 19:34

1 Answers1

10

In C#, this is an expression, not a statement. As a complete statement, it won't compile. It would be fine in Perl, JavaScript, and probably other languages, but syntax is arbitraryish so you can't reliably generalize from one language to another.

a ? b : c;

It isn't an "if statement"; it's a conditional expression. This is your code with an if statement; I expect that this version will compile for you, but let me know.

services.AddConfiguration(builder =>
{
    if (_settings.Value.UsePostgreSQL) {
        builder.UseSqlServer(_settings.Value.ConnectionString, 
            options => options.MigrationsAssembly(migrationsAssembly));
    } else {
        builder.UseNpgsql(_settings.Value.ConnectionString, 
            options => options.MigrationsAssembly(migrationsAssembly));
    }
});

It looks like services.AddConfiguration() expects an Action<T>, not a Func<T>, so the compiler will require the body of the lambda to be a statement, not an expression.

This is a statement containing a conditional expression, and it will compile:

var d = a ? b : c;

That's what the conditional operator is for, but it's not what you were trying to do.