Tutorial :Simple MVC NerdDinners Lambda


In this code from Microsoft's MVC Tutorial NerdDinners:

public class DinnerRepository {

private NerdDinnerDataContext db = new NerdDinnerDataContext();    //  // Query Methods    public IQueryable<Dinner> FindAllDinners() {      return db.Dinners;  }    public IQueryable<Dinner> FindUpcomingDinners() {      return from dinner in db.Dinners             where dinner.EventDate > DateTime.Now             orderby dinner.EventDate             select dinner;  }    public Dinner GetDinner(int id) {      return db.Dinners.SingleOrDefault(d => d.DinnerID == id);  }    //  // Insert/Delete Methods    public void Add(Dinner dinner) {      db.Dinners.InsertOnSubmit(dinner);  }    public void Delete(Dinner dinner) {      db.RSVPs.DeleteAllOnSubmit(dinner.RSVPs);      db.Dinners.DeleteOnSubmit(dinner);  }    //  // Persistence     public void Save() {      db.SubmitChanges();  }   


What does:

public Dinner GetDinner(int id) {      return db.Dinners.SingleOrDefault(d => d.DinnerID == id);  }  

the "d" mean? How does this code work? I know it it bringing back dinners where dinnerid matches id from the function parameter. I don't understand the "d goes to..." means. I know it is a lambda but I don't really get it. What is the "d" for? What does it do?

Could this have been written without the lambda here (how)?


You should probably read up on anonymous methods.

Basically the code you are referring to can be written as an anonymous method without lamba syntax like this:

public Dinner GetDinner(int id) {         return db.Dinners.SingleOrDefault(delegate (Dinner d) {                                         return d.DinnerID == id;                                       });  }  


This is similar too...

from d in db.Dinners  where d.DinnerID == id  select d  

The code basically loops around the dinners returning the first Dinner or the default if none is found.

This is a case where the naming conventions used in a sample aren't always appropriate in production. Using a "d" as a local variable is usually fround upon and choosing a variable name of "dinner" would probably be more appropriate, although in this case the scope of d is so small it is clear either way, as long as you know how lambda expressions work.


You need to understand lambda syntax and what it's used for.

Here's an article that does a decent job of explaining it.

However, to shortly answer your question in regards to the NerdDinner context, "d" in this context is just a parameter passed into the lamda expression that is a Dinner object.


This bit of code:

d => d.DinnerID == id  

Can be thought of as defining a function of type Func<Dinner, bool>.

Whatever you pass it to, this function can be called, and passed Dinner, and it will give back a bool.

void Foo(Func<Dinner, bool> f)  {      bool result = f(new Dinner());  }  

In your real example, the function SingleOrDefault will call the function you give it multiple times, passing it a different Dinner each time, and will return the Dinner for which the function returns true.

In fact, as you're using IQueryable, this is only conceptually what happens. The chances are, the code of the function is converted into SQL, and all the execution is done inside the database.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »