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表达式格式为:(参数列表)=>表达式或语句块lambda

解决方案 »

  1.   

    写一个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作为参数传过来的。
      

  2.   

    想了想似乎觉得,像 b => b.xxx 只能用在特定环境之中,才能解释得通。在EF中 此处b可能被编译器隐式指上book实体。
      

  3.   

    接触不多,我以前理解成select ISBN from table b
    我也说不清楚。
      

  4.   


    先谢谢版主大哥。 我理解了,关于此处的泛型方法Func<string, bool>的两个参数是否是固定指一个参数输入另一个作返回值?
      

  5.   

    对,就是 bool where(string s)
      

  6.   

    先忘了啥lambda,匿名委托把先去看什么是委托 理解 委托的函数原型是什么,你知道函数原型了,自然就明白了。ps:微软webcast里有个讲师叫“李建忠”在net3刚出来的时候,弄过一个《C# 3.0 锐利体验系列课程》,自己下了看把,讲的不错,他一开始就并不是直接从语法表象上出发,他一开始就告诉你一条非常正确的途径--应该先在net2的基础上去看“语法糖”
      

  7.   

    那套webcast的地址
    http://msdnwebcast.net/webcast/1/2170/