Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ naming Standard - Lambda Expression [duplicate]

We normally follow coding / naming standard for all C# syntax. For Example, if we declare string inside the method, we use Scope-datatype-FieldName format. (lstrPersonName)

List<Person> icolPerson;
private LoadPersonName()
{
   string lstrPersonaName;
}

i am kind of thinking how do we follow the naming standard in Lambda Expression. Especially when we defined the arguments for func delegate, we use shorted names like x. For Example

var lobjPerson = icolPerson.Where(x => x.person_id = 100)

if you look at the above line, X does not follow any standard. i also read that one purpose of lambda expression is to reduce the lenghthy code.

i am here to seek help to get the right approach for naming standard.

var lobjPerson = icolPerson.Where(x => x.person_id = 100)

OR

var lobjPerson = icolPerson.Where(lobjPerson => lobjPerson .person_id = 100)
like image 260
Jeeva Subburaj Avatar asked Nov 03 '09 10:11

Jeeva Subburaj


People also ask

What does => mean in LINQ?

the operator => has nothing to do with linq - it's a lambda expression. It's used to create anonymous functions, so you don't need to create a full function for every small thing.

What is the use of => in C#?

The => token is supported in two forms: as the lambda operator and as a separator of a member name and the member implementation in an expression body definition.

Is LINQ faster than lambda?

In some cases LINQ is just as fast if not faster than other methods, but in other cases it can be slower. We work on a project that we converted to linq and the data lookup is faster but the merging of data between two tables is much slower.

What is lambda expression in LINQ?

A lambda expression is a convenient way of defining an anonymous (unnamed) function that can be passed around as a variable or as a parameter to a method call. Many LINQ methods take a function (called a delegate) as a parameter.


1 Answers

My lambdas use one letter arguments, usually the first letter of what the parameter name would be:

  • buttons.Select(b => b.Text);

  • services.Select(s => s.Type);

But sometimes I add a few more letters to make things clearer, or to disambiguate between two parameters.

And when there isn't much meaning attached to the things I use xs and ys:

  • values.Aggregate((x, y) => x + y);

All said, the standard I use for lambdas is shortness first, expressiveness later, because the context tends to help understand things (in the first example it's obvious that the b stands for a button).

like image 185
R. Martinho Fernandes Avatar answered Sep 28 '22 04:09

R. Martinho Fernandes