LINQ select Clause

The select clause in LINQ shapes the info to be enclosed within the question result victimisation projection. Projection is that the method of reworking AN object into a replacement type.The select clause of a LINQ query must be placed at the end of the query. You can do several forms of manipulations to the presently queried price, retrieve bound properties of it, or produce associate anonymous sort supported the queried object’s properties. A normal LINQ inquiry restores an accumulation that executes the IEnumerable<T> interface where T is the subsequent information sort of the articulation in the select proclamation.For instance, if the select clause shows to recover each individual in a Person collection, at that point the returned gathering executes the IEnumerable<Person> interface.In the event that the  select statement just chooses the FirstName of each individual, and expecting the FirstName property is of type  string, at that point the returned result actualizes the IEnumerable<string>interface. You would then be able to utilize a foreach  circle to begin the recovering of results that will be yielded by the inquiry articulation.

You can simply select the whole queried object by using the value of the range variable. This will select the queried object without any modifications or variations.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var results = from number in numbers
              select number;

foreach (var n in results)
{
     Console.Write("{0, -2}", n);
}
1 2 3 4 5 6 7 8 9 10

You can use different kinds of expressions that yield a result during selection. For example, you can query every number in an array of numbers and increment each of them by one.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var results = from number in numbers
              select number + 1;

foreach (var n in results)
{
     Console.Write("{0, -2}", n);
}
2 3 4 5 6 7 8 9 10 11

You can also select individual properties or combination of properties during the selection process. For example, you can only select the FirstName of every Person object from a List of Persons.

List<Person> people = new List<Person> 
{
    new Person("Johnny", "Smith"),
    new Person("Mark", "Lawrence"),
    new Person("Jessica", "Fisher"),
    new Person("Danny", "Mayer"),
    new Person("Raynold", "Alfonzo")
};

var firstNames = from person in people
                 select person.FirstName;
Johnny
Mark
Jessica
Danny
Raynold

You can even combine different properties or other external data to the final value for the selection. For example, you can combine the FirstName property and the LastName property to select every person’s full name.

var fullNames = from person in people
 select person.FirstName + " " + .person.LastName;
Johnny Smith
Mark Lawrence
Jessica Fisher
Danny Mayer
Raynold Alfonzo

The above choose clause can select the person’s FirstName, followed by house, and so his/her LastName. you’ll use ways to control the ultimate worth to be elect. For example, suppose you want to select every FirstName and convert them into all caps.

var namesInCaps = from person in people
                  select person.FirstName.ToUpper();
JOHNNY
MARK
JESSICA
DANNY
RAYNOLD

You can even select values not related to the data source being queried. For example, we can have a collection of indices and select a value from another collection or array.

int[] indices = { 0, 1, 2, 3, 4 };
string[] words = { "Example1", "Example2", "Example3", "Example4", "Example5" };

var result = from index in indices
             select words[index];
Example1
Example2
Example3
Example4
Example5

We can create anonymous types during selection and include certain properties of each object. For example, suppose we have a Personobject that has FirstNameLastNameAge, and Gender properties, and we only want to retrieve their FirstName and LastName, we can create a new anonymous type in the select clause which only includes the FirstName and LastName properties.

var annonymous = from person in people
                 select new { person.FirstName, person.LastName };

foreach (var p in annonymous)
{
    Console.WriteLine(p.FirstName + " " + p.LastName);
}

The sort of the chosen result would be associate degree anonymous type having the properties enclosed in the select clause.You can even create new properties based on the properties of the queried object.

var results = from person in people
              select new { FN1 = person.FirstName, LN1 = person.LastName };

foreach (var p in results)
{
    Console.WriteLine(p.FN1 + " " + p.LN1);
}
Johnny Smith
Mark Lawrence
Jessica Fisher
Danny Mayer
Raynold Alfonzo

The select clause of the question expression higher than assigns the values of FirstName and LastName properties to new properties with completely different names. The type of these new properties are determined by the compiler based on the values being assigned to them thanks to type inference. These new properties are absorbed by the created anonymous type.

var results = from person in people
              select new { Upper = person.FirstName.ToUpper(), 
               Lower = person.FirstName.ToLower() }; 

foreach (var p in results)
{
   Console.WriteLine("Upper={0}, Lower={1}", p.Upper, p.Lower);
}
Upper=JOHNNY, Lower=johnny
Upper=MARK, Lower=mark
Upper=JESSICA, Lower=jessica
Upper=DANNY, Lower=danny
Upper=RAYNOLD, Lower=raynold

The select clause higher than assigns the FirstName converted to majuscule to a replacement property named Upper, Associate in Nursingd it’s minuscule version to the new Lower property that ar then enclosed in an anonymous kind. We then used these new properties inside a foreach loop.

var results = from person in people
              select new { FullName = person.FirstName + " " + person.LastName };

foreach (var p in results)
{
   Console.WriteLine(p.FullName);
}
Johnny Smith
Mark Lawrence
Jessica Fisher
Danny Mayer
Raynold Alfonzo

The above query’s select clause creates a new property named FullName and assigns the combined value of each person’s FirstNameand LastName. The resulting anonymous type will have the FullName property which automatically gets the full name of the person.

var results = from person in people
              select new { person.Age, FullName = person.FirstName + " " + person.LastName };

foreach (p in results)
{
    Console.WriteLine("FullName={0}, Age={1}", p.FullName, p.Age);
}
FullName=Johnny Smith, Age=22
FullName=Mark Lawrence, Age=24
FullName=Jessica Fisher, Age=19
FullName=Danny Mayer, Age=21
FullName=Raynold Alfonzo, Age=25

The preceding question contains a select clause creates AN anonymous sort with a mix of an explicit property of every person and a brand new property FullName which contains the combined firstname and lastname of every person.