No, this is optional.
We have two ways to record LINQ queries.
One is a query method and the other is a construction method. You only need to add the lambda expression in the case of the build method. For example, if we want to find all these students from the Students object that have more grades than 70. but we can do this in LINQ with the following syntax
var data = from p in stdList where p.marks > 70 select p;
or var data = stdList.Where (p => p.marks> 70);
a later approach is a construction method, in the Where function we pass lambda expressions.
Lambda expressions are just short abbreviations that you can always use in LINQ queries, but if you want to avoid the syntax of the whole query to just apply a simple condition, you can simply use LINQ building methods (which query lambda expressions) in lambda expressions , you always define some alias and then do your operation.
As for the => operator, it works just like an assignment operator. For example:
(p) => p.Gender == "F" It means "All persons p, such that person's Gender is F"
In some literature, this is called a predicate. Another literary terminology is Projection.
(p) => p.Gender ? "F" : "Female" "Each person p becomes string "Female" if Gender is "F""
This is a projection, it uses a ternary operator. Although I have explained very simple examples, but I hope this helps you., :)
Madiha khalid
source share