C# Keywords Tutorial Part 37: from
Amr Saafan
Founder | CTO | Software Architect & Consultant | Engineering Manager | Project Manager | Product Owner | +27K Followers | Now Hiring!
Popular programming language C# is frequently employed in the creation of contemporary apps. The “from” keyword, which may be used in LINQ (Language Integrated Query) queries to get data from collections or other data sources, is one of the features of the C# programming language. We will examine the “from” keyword and its applications in C# in this blog article.
LINQ queries may introduce range variables by using the “from” keyword. Each component of the collection or data source that is being searched is represented by a range variable. The “from” keyword has the following syntax when used in a LINQ query:
from rangeVariable in dataSource
select rangeVariable
In the above syntax, “rangeVariable” represents the current element being iterated over in the query, and “dataSource” represents the collection or data source being queried. The “select” keyword is used to project the range variable into a new form, which could be a different type, a subset of the original type, or some other transformation.
Let’s look at a few examples of using the “from” keyword in C#.
领英推荐
Example 1: Filtering a collection
int[] numbers = { 1, 2, 3, 4, 5 };
var query = from number in numbers
? ? ? ? ? ? where number > 3
? ? ? ? ? ? select number;
foreach (var number in query)
{
? ? Console.WriteLine(number);
}
In this example, we have an array of integers called “numbers”. We use the “from” keyword to create a range variable called “number” that represents each element in the “numbers” array. We then use the “where” keyword to filter the elements in the array to those that are greater than 3. Finally, we use the “select” keyword to project the range variable into a new form, which in this case is the same integer value. The result of this query is a collection of integers that contains only the values 4 and 5, which are greater than 3. We then use a foreach loop to iterate over the result and print each number to the console.
Example 2: Joining two collections
var customers = new List<Customer>
{
? ? new Customer { Id = 1, Name = "Alice" },
? ? new Customer { Id = 2, Name = "Bob" },
? ? new Customer { Id = 3, Name = "Charlie" }
};
var orders = new List<Order>
{
? ? new Order { Id = 1, CustomerId = 1, Total = 100 },
? ? new Order { Id = 2, CustomerId = 2, Total = 200 },
? ? new Order { Id = 3, CustomerId = 3, Total = 300 },
? ? new Order { Id = 4, CustomerId = 1, Total = 400 },
? ? new Order { Id = 5, CustomerId = 2, Total = 500 }
};
var query = from customer in customers
? ? ? ? ? ? join order in orders on customer.Id equals order.CustomerId
? ? ? ? ? ? select new { customer.Name, order.Total };
foreach (var item in query)
{
? ? Console.WriteLine($"Customer: {item.Name}, Order Total: {item.Total}");
}
In this example, we have two collections: a list of customers and a list of orders. We use the “from” keyword to create range variables for each collection, “customer” and “order”. We then use the “join” keyword to join the two collections on the “CustomerId” property of the order and the “Id” property of the customer. We use the “equals” keyword to specify the equality condition for the join.