understanding C# Lambda Expressions

2019-09-21 10:57发布

I have googled a lot to understand the lambda expressions in c#. Though people have given a handful of information, I couldn't understand what it is. Can any one explain me with the following code. This code can give me the understanding, since it is "my" context.

context.Authors.AddOrUpdate(x => x.Id,
    new Author() { Id = 1, Name = "Jane Austen" },
    new Author() { Id = 2, Name = "Charles Dickens" },
    new Author() { Id = 3, Name = "Miguel de Cervantes" }
    );

Why it is not "x=>x" ?

标签: c# lambda
3条回答
狗以群分
2楼-- · 2019-09-21 11:14
x => x 

is a shorthand for

x => {
    return x;
}

which is a function that takes x as a parameter and returns x, while

x => x.Id

is a shorthand for

x => {
    return x.Id;
}

This basically means that the AddOrUpdate function needs to know how to get the Id of the entities it's adding or updating, you can think of lambdas as a compact way of defining functions, in most cases you can actually define a function:

int GetAuthorId(Author x) {
    return x.Id;
}

and use the function in place of the lambda:

context.Authors.AddOrUpdate(GetAuthorId,
    new Author() { Id = 1, Name = "Jane Austen" },
    new Author() { Id = 2, Name = "Charles Dickens" },
    new Author() { Id = 3, Name = "Miguel de Cervantes" }
);

Inside the AddOrUpdate function, it will run the GetAuthorId passing an Author as a parameter whenever it wants to find an Author's Id

--EDIT--

As correctly noted in the comments, what I just said is true for Func<>, but is NOT true for Expression<Func<>> you can read a little about the difference here

查看更多
小情绪 Triste *
3楼-- · 2019-09-21 11:21

Simply stated, it's because the method is expecting to receive an expression that represents the semantic key for Authors.

Think of it like this - if you passed in x => x, you would be trying to perform the following operation:

  1. Based upon the key specified by the object Author
  2. Add or update the given Author objects.

Whereas when you pass in x => x.Id, you're saying:

  1. Based upon the key specified by the object Author.Id
  2. Add or update the given Author objects.

To understand why, it may help you note the method takes an Expression<Func<TEntity,Object>>. This parameter type is often use to perform operations upon an arbitrary property, like AddOrUpdate is doing here.

Here's an example of a method would takes a expression and prints out the member name of the property. There's a lot more power here, but this should atleast help explain why AddOrUpdate() takes the parameters it does.

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

static void Example<T>(Expression<Func<T, object>> identifierExpression)
{
    MemberExpression prop = identifierExpression.Body as MemberExpression;
    Console.WriteLine(prop.Member);
}

static void Main(string[] args)
{
    var person = new Person { FirstName = "jd", LastName = "phenix" };
    Example<Person>(x => x.FirstName);
}
查看更多
forever°为你锁心
4楼-- · 2019-09-21 11:29
Why it is not "x=>x"

Because the function wants sometime to uniquely identify each object. In this case the ID of the object.

If the ID exists, an update is performed. If the ID doesn't exist, an insert is performed.

查看更多
登录 后发表回答