1

I am new to the Entity Framework and while I have the basics down, I am stumbling over a specific syntax that I do not understand. The code works, but it is a bit of a "black box" to me and I fee a bit hampered by not understanding it.

I have a local private variable declared in my class as thus:

private clientexperienceEntities ceContext;

Farther in my code I instantiate it thus:

ceContext = new clientexperienceEntities();

This next line is the portion I am having difficulty with:

var client = ceContext.clients.First(a => a.ID == _ID);

Specifically the parameters to the First method, what exactly does a => a.ID == _ID mean? I know it is telling the context to find the first matching record based on the primary key value contained in the _ID value. But I don't understand where the 'a' comes from, or whatever other name I want to use, 'b' or 'cat'.

Is this syntax part of Linq? I am not even sure what exactly to search for to understand it.

As I said the statement works, and I can the manipulate the entity returned, but I just don't fully understand that parameter construct.

akjoshi
  • 15,374
  • 13
  • 103
  • 121

3 Answers3

5

You should learn more about lambda expression

Lambda expression is an inline delegate introduced with C # 3.0 language. It’s a concise way to represent an anonymous method.

To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side. In this case: a => a.ID == _ID specifies a parameter type of ClientExperience that’s named a and returns true if a.ID is equal to _ID.

phnkha
  • 7,782
  • 2
  • 24
  • 31
  • Excellent! Thank you! At least now I know where to look. From a quick peek it appears to be a shortcut to building expression trees. Not something I have looked into in depth before. I appreciate the feedback and pointer. – rbruceporter Jan 28 '13 at 07:15
2

I find it helps if I narrate the expression for myself. If I were to write or read that line, it would sound like such:

var client IS the FIRST client IN ceContext which is the result of the equation (a WHERE a.ID EQUALS _ID);

Yes, you can use any name for your Lambda variable. It's just like writing a mathematical expression. You can solve for x, a, y, xxx, cat or whatever.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68
1

what exactly does a => a.ID == _ID mean?

It is a labmda expression and acts as a filter.

This is identical do - more clear for someone who did not read the documentation:

clients.Where(x => x.ID == _ID).First ();

The expression compares the field ID (x.ID) on the object X (which is every object in the query) against the value of _ID (which has to be a local variable, naming would indicate so).

It is a filter. Get me first item where the FIeld ID is the value of the variable ID.

Reading up on Lambda Expressions is needed here - you will not get far without understanding them.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • This makes a lot of sense, thank you and the others for assisting me in both identifying the technology aspect (lambda expressions) and helping to understand what I was looking at. – rbruceporter Jan 28 '13 at 07:18