Projection Operators
A projector machine ,projects slides or film on to a screen , means a projector transforms one form of things into other similarly In LINQ, projection means take all or some properties of an object ,either modify them or don't modify them and project them into a new type ,same type or an anonymous type.
follow below examples for better understanding
Example 1. Projecting object property's into different Object
In the above example we are projecting Employee's properties to Contact Object which have just three properties ContactId , Phone and PersonName
Example 2.Projecting object property's into anonymous Object
In the above example we are projecting properties of Employee Object to an unnamed (anonymous) object , the anonymous object have three properties Id, PhoneNumber and Person.
Example 3. Projecting object property's into Same Object
Example 4. Projecting object property's into new Object with modification
Notice in the above example we are computing PersonName property of Contact class object based on the MartialStatus and Gender properties of Employee object , I have used nested ternary operator to achieve below requirement.
• Gender="Female" and "MaritalStatus"= "Married" then Add "Mrs." prefix.
• Gender="Female" and "MaritalStatus" = UnMarried then Add "Ms." prefix else
• Add "Mr." prefix to the name property's value.
Types of LINQ Projection Operators
Linq provides us two flavours of projection operators and obviously each one have its own purpose.
• Select and
• Select many
In the coming chapters, we will learn all these Projection operators in detail with examples.