简单 MVC NerdDinners Lambda

发布于 2024-07-19 19:17:36 字数 1177 浏览 2 评论 0 原文

在 Microsoft 的 MVC 教程 NerdDinners 的这段代码中:

公共类DinnerRepository{

private NerdDinnerDataContext db = new NerdDinnerDataContext(); 

  // 
  // 查询方法 

  公共 IQueryable<晚餐>   查找所有晚餐(){ 
      返回 db.Dinners; 
  } 

  公共 IQueryable<晚餐>   查找即将到来的晚餐(){ 
      从 db.Dinners 吃完晚饭回来 
             其中晚餐.EventDate >   日期时间.现在 
             按晚餐订购。EventDate 
             选择晚餐; 
  } 

  公共晚餐 GetDinner(int id) { 
      return db.Dinners.SingleOrDefault(d => d.DinnerID == id); 
  } 

  // 
  // 插入/删除方法 

  公共无效添加(晚餐晚餐){ 
      db.Dinners.InsertOnSubmit(晚餐); 
  } 

  公共无效删除(晚餐晚餐){ 
      db.RSVPs.DeleteAllOnSubmit(晚餐.RSVPs); 
      db.Dinners.DeleteOnSubmit(晚餐); 
  } 

  // 
  // 持久化  

  公共无效保存(){ 
      db.SubmitChanges(); 
  }  
  

}

的作用是:

公共晚餐 GetDinner(int id) { 
      return db.Dinners.SingleOrDefault(d => d.DinnerID == id); 
  } 
  

这个“d”是什么意思? 这段代码是如何工作的? 我知道它会带回晚餐,其中dinnerid 与函数参数中的id 匹配。 我不明白“d 去...”的意思。 我知道它是 lambda 但我不太明白。 “d”是什么意思? 它有什么作用?

是否可以在没有 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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

腻橙味 2024-07-26 19:17:36

您可能应该阅读匿名方法

基本上,您所指的代码可以编写为匿名方法,无需使用 lba 语法,如下所示:

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

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;
                                     });
}
深海不蓝 2024-07-26 19:17:36

这也很相似...

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

代码基本上围绕晚餐循环,返回第一个晚餐,如果没有找到,则返回默认值。

在这种情况下,示例中使用的命名约定并不总是适合生产。 使用“d”作为局部变量通常是常见的,选择变量名称“dinner”可能更合适,尽管在这种情况下 d 的范围很小,只要您知道,无论如何都是清楚的lambda 表达式如何工作。

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.

聚集的泪 2024-07-26 19:17:36

您需要了解 lambda 语法及其用途。

这里有一篇文章很好地解释了这一点。

然而,为了简短地回答您关于 NerdDinner 上下文的问题,在此上下文中的“d”只是传递到作为晚餐对象的 lamda 表达式的参数。

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.

予囚 2024-07-26 19:17:36

这段代码:

d => d.DinnerID == id

可以被认为定义了一个 Func 类型的函数。

无论您将其传递给什么,都可以调用此函数并传递Dinner,并且它将返回一个bool

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

在您的真实示例中,函数 SingleOrDefault 将多次调用您为其提供的函数,每次传递不同的 Dinner ,并返回 Dinner code> 函数返回 true

事实上,当您使用 IQueryable 时,这只是概念上所发生的情况。 很可能,该函数的代码被转换为 SQL,并且所有执行都在数据库内完成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文