locked
Stuck with unclear lambda expression usage in asp.net core 2 beginners course on MVA RRS feed

  • Question

  • I searched about the usage of lambda expression 

    I understand the way of it , its simple to use and very helpful

    but , the usage of a lambda expression directly in the top of a function invokation parens and initializing a properties of a given variable isn't really clear and the instructor is just moving on !

    could not figure it out about what happened tbh , so here i am asking here 

    how he is just giving a lambda function that takes a variable and this variable unfortunately has a specific type and properties !

    My accurate question is :

    • From where that type is sent to the variable "options"
    • how a function can be declared to do such a thing ? 

    I hope to get a clear answer , I have asked before in a groups and no one explained the exact point of what is really happening there 

    • Moved by CoolDadTx Thursday, February 15, 2018 3:38 PM ASP.NET related
    Thursday, February 15, 2018 12:05 PM

Answers

  • Have a look at the definition here.

    One of the arguments to the AddDbContext() method is an Action<DbContextOptionsBuilder>.  And if you are not sure what an Action is, then look here

    Essentially its asking for an argument that is, itself, a method that takes one argument of type DbContextOptionsBuilder.

    You could write something like the following, where you explicitly write the method then pass it in:

    public void MySetOptions(DbContextOptionsBuilder options)
    {
      options.UseInMemoryDatabase("name");
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<AppDbContext>(MySetOptions);
    
        services.AddMvc();
    }

    But the code you showed in your post is using a lambda expression instead as a shortcut.

    • Marked as answer by NickB09 Saturday, February 17, 2018 8:26 AM
    Thursday, February 15, 2018 1:31 PM

All replies

  • Questions about ASP should be posted in the ASP.NET forums:

    https://forums.asp.net/

    - Wayne

    Thursday, February 15, 2018 1:04 PM
  • Have a look at the definition here.

    One of the arguments to the AddDbContext() method is an Action<DbContextOptionsBuilder>.  And if you are not sure what an Action is, then look here

    Essentially its asking for an argument that is, itself, a method that takes one argument of type DbContextOptionsBuilder.

    You could write something like the following, where you explicitly write the method then pass it in:

    public void MySetOptions(DbContextOptionsBuilder options)
    {
      options.UseInMemoryDatabase("name");
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<AppDbContext>(MySetOptions);
    
        services.AddMvc();
    }

    But the code you showed in your post is using a lambda expression instead as a shortcut.

    • Marked as answer by NickB09 Saturday, February 17, 2018 8:26 AM
    Thursday, February 15, 2018 1:31 PM
  • As already mentioned, please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 15, 2018 3:38 PM