Now, let's combine projection, filtering, and aggregation to see the collective power of these techniques. We'll extend our example to demonstrate this flow:
- Data Projection: Choose only the desired fields.
- Data Filtering: Filter the data based on certain conditions.
- Data Aggregation: Summarize the filtered data.
We'll modify our DataStream class to include all the methods and then use them together in a workflow. On top of that, projection and filtering methods will now return an instance of DataStream<T>, not a list as before, so that we can chain these methods when calling them:
using System;
using System.Collections.Generic;
using System.Linq;
public class DataStream<T>
{
public List<T> Data { get; set; }
public DataStream(List<T> data)
{
Data = data;
}
public DataStream<dynamic> ProjectData(List<string> keys)
{
var projectedData = Data.Select(d =>
{
var newObj = new Dictionary<string, object>();
foreach (var key in keys)
{
var propertyInfo = d.GetType().GetProperty(key);
newObj[key] = propertyInfo?.GetValue(d, null);
}
return (dynamic)newObj;
}).ToList();
return new DataStream<dynamic>(projectedData);
}
public DataStream<T> FilterData(Func<T, bool> testFunc)
{
var filteredData = Data.Where(testFunc).ToList();
return new DataStream<T>(filteredData);
}
public double AggregateData(string key, Func<List<dynamic>, double> aggFunc)
{
var values = Data.Select(d =>
{
var propertyInfo = d.GetType().GetProperty(key);
return (dynamic)propertyInfo?.GetValue(d, null);
}).ToList();
return aggFunc(values);
}
}
public class Program
{
public static void Main()
{
var ds = new DataStream<dynamic>(new List<dynamic>
{
new { Name = "Alice", Age = 25, Profession = "Engineer", Salary = 70000 },
new { Name = "Bob", Age = 30, Profession = "Doctor", Salary = 120000 },
new { Name = "Carol", Age = 35, Profession = "Artist", Salary = 50000 },
new { Name = "David", Age = 40, Profession = "Engineer", Salary = 90000 },
});
// Step 1: Project the data to include only 'Name', 'Age', and 'Salary'
var projectedDs = ds.ProjectData(new List<string> { "Name", "Age", "Salary" });
// Step 2: Filter the projected data to include only those with age > 30
var filteredDs = projectedDs.FilterData(x => x.Age > 30);
// Step 3: Aggregate the filtered data to compute the average salary
var averageSalary = filteredDs.AggregateData("Salary", salaries => salaries.Average(v => (double)v));
Console.WriteLine(averageSalary); // Outputs: 70000.0
}
}
Here:
- Projection: We choose only the
Name, Age, and Salary fields from our data. The ProjectData method now returns a DataStream<dynamic> object, allowing us to chain multiple operations.
- Filtering: We filter the projected data to include only those persons whose age is greater than 30. The
FilterData method also returns a DataStream<T> object for chaining.
- Aggregation: We calculate the average salary of the filtered data. The final output shows the average salary for those aged over 30, which is
70,000.
By combining these methods, our data manipulation becomes both powerful and concise. Try experimenting and see what you can create!