-2

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" ?

Muthu Vel
  • 67
  • 9
  • 1
    Have you read the [documentation](https://msdn.microsoft.com/en-us/library/hh846514(v=vs.103).aspx) for this method? – DavidG Dec 05 '15 at 02:18
  • ... and also the documentation on [Lambda expressions](https://msdn.microsoft.com/en-us/library/bb397687.aspx). – sstan Dec 05 '15 at 02:20
  • 1
    I'd say that's a pretty involving method for 'understanding' Lambda expressions and will require the understanding of expressions too. I would suggest looking at LINQ – William Dec 05 '15 at 02:20

3 Answers3

4
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

Community
  • 1
  • 1
Eduardo Wada
  • 2,606
  • 19
  • 31
  • 3
    OP is probably referring to [IDbSetExtensions.AddOrUpdate](https://msdn.microsoft.com/en-us/library/hh846514(v=vs.103).aspx). If so, the first parameter is an expression, not a delegate, in which case, the lambda expression can't be substituted by a function. – sstan Dec 05 '15 at 02:37
1

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);
}
jdphenix
  • 15,022
  • 3
  • 41
  • 74
0
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.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69