如果理解EF中的Lambda表达式?

perock 2013-01-04 11:58:25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.ModelConfiguration;

namespace EFCodeFirstWalkthroughModel
{
public class BookConfiguration : EntityConfiguration<Book>
{
public BookConfiguration()
{
this.HasKey(b => b.ISBN);
this.Property(b => b.Title).IsRequired();
this.HasRequired(b => b.Author).WithMany(a => a.Books);
}
}
}


例如上述代码之中,使用了Lambda表达式: this.HasKey(b => b.ISBN);
此处,b的值是由谁赋予的?在什么时候赋值?

另外在百度上找了一些解释例子,Lambda表达式相当于匿名委托,更加看不明白了,下面s是怎么赋值的?


var inString = list.FindAll(delegate(string s) {return s.Indexof("YJingLee") >= 0; });
使用Lambda表达式代码
var inString = list.FindAll(s => s.Indexof("YJingLee") >= 0);
Lambda表达式格式为:(参数列表)=>表达式或语句块



...全文
201 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
perock 2013-01-04
  • 打赏
  • 举报
回复
谢谢,可以结贴了!!
wanghui0380 2013-01-04
  • 打赏
  • 举报
回复
那套webcast的地址 http://msdnwebcast.net/webcast/1/2170/
wanghui0380 2013-01-04
  • 打赏
  • 举报
回复
先忘了啥lambda,匿名委托把 先去看什么是委托 理解 委托的函数原型是什么,你知道函数原型了,自然就明白了。 ps:微软webcast里有个讲师叫“李建忠”在net3刚出来的时候,弄过一个《C# 3.0 锐利体验系列课程》,自己下了看把,讲的不错,他一开始就并不是直接从语法表象上出发,他一开始就告诉你一条非常正确的途径--应该先在net2的基础上去看“语法糖”
threenewbee 2013-01-04
  • 打赏
  • 举报
回复
对,就是 bool where(string s)
perock 2013-01-04
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
写一个FindAll的模拟代码帮助你理解: List<string> FindAll(List<string> data, Func<string, bool> where) { List<string> result = new List<string>(); foreach (string item in data) { ……
先谢谢版主大哥。 我理解了,关于此处的泛型方法Func<string, bool>的两个参数是否是固定指一个参数输入另一个作返回值?
我就是大神 2013-01-04
  • 打赏
  • 举报
回复
接触不多,我以前理解成select ISBN from table b 我也说不清楚。
perock 2013-01-04
  • 打赏
  • 举报
回复
想了想似乎觉得,像 b => b.xxx 只能用在特定环境之中,才能解释得通。在EF中 此处b可能被编译器隐式指上book实体。
threenewbee 2013-01-04
  • 打赏
  • 举报
回复
写一个FindAll的模拟代码帮助你理解: List<string> FindAll(List<string> data, Func<string, bool> where) { List<string> result = new List<string>(); foreach (string item in data) { if (where(item)) result.Add(item); //注意item是where的实参 } return result; } 调用 List<string> data = new List<string>() { "1", "1", "2", "2" }; List<string> result = FindAll(data, x => x == "1"); // result: "1" "1",注意x是过滤条件的形参。它哪里来的?FindAll调用它的时候把item作为参数传过来的。

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧