LINQ Compound from Clauses using C# in ASP.Net
In LINQ, the compound from clauses enables to apply the query over the inner list sequence of provided data source sequence. Let's understand the usability with an example of Student class. The data source may be in the form of IEnumerable<Student> where each object of student class contains a list of marks. The element Marks of the student object is an inner sequence that can be accessed using compound from clause. The use of compound from clauses is like using nested foreach loop over the sequence. You can also use the where clause and orderby clause to filter and sort the resulting sequence. The following C# example in ASP.Net shows the use of compound from clause that will access the inner list of student's marks also.
Example for LINQ Compound from Clauses using C# in ASP.Net
List<Student> Students = new List<Student>()
{
new Student(101, "Shawn", "Johnson", new List<int>() { 91, 88, 76, 93 }),
new Student(102, "Rick", "Adams", new List<int>() { 70, 73, 66, 90 }),
new Student(103, "Smith", "Wimothy", new List<int>() { 73, 80, 75, 88 }),
new Student(104, "Michael", "Pinto", new List<int>() { 82, 75, 66, 84 }),
new Student(105, "Peter", "Barrows", new List<int>() { 67, 78, 70, 82 })
};
var query = from student in Students
from marks in student.Marks
where marks < 80 && marks > 70
select new { FirstName = student.FirstName, LastName = student.LastName, Marks = marks };
foreach (var student in query)
{
Response.Output.Write("Student: {0} {1}; Marks: {2}<br />", student.FirstName, student.LastName, student.Marks);
}
// Output:
// Student: Shawn Johnson; Marks: 76
// Student: Rick Adams; Marks: 73
// Student: Smith Wimothy; Marks: 73
// Student: Smith Wimothy; Marks: 75
// Student: Michael Pinto; Marks: 75
// Student: Peter Barrows; Marks: 78
The Student class:
public class Student
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<int> Marks { get; set; }
public Student(int Id, string firstName, string lastName, List<int> marks)
{
this.ID = Id;
this.FirstName = firstName;
this.LastName = lastName;
this.Marks = marks;
}
}
In the above C# example we have used the two from clauses. The first from clause for getting the values of Student data source list sequence and the second from clause for getting the values of inner list sequence of marks element. We have also used a where clause with second from clause to filter the marks.
Continue to next tutorial: LINQ Multiple from Clauses using C# in ASP.Net to learn how to use the multiple from clauses to perform the cross join operation.

* will not be published
* hint: http://www.example.com