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.
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.