C# > Advanced C# > LINQ > Working with Anonymous Types
LINQ with Anonymous Types: Grouping and Projection
This code snippet demonstrates how to use LINQ with anonymous types to group data and project it into a new, custom shape without defining a specific class. It showcases the power and flexibility of anonymous types for data transformation.
Code Example: Grouping Products by Category and Creating Anonymous Type
This C# code snippet demonstrates the usage of LINQ with anonymous types to group a list of 'Product' objects by category. The 'GroupBy' method groups the products based on their 'Category' property. The 'Select' method then transforms each group into an anonymous type. This anonymous type contains the 'CategoryName', 'ProductCount', 'TotalPrice' of products in that category, and a nested projection to display the names and prices of each product in the category. The resulting 'groupedProducts' variable holds a collection of these anonymous types. The final loop iterates through the grouped data and prints the relevant information to the console.
using System;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
List<Product> products = new List<Product>
{
new Product { Name = "Laptop", Category = "Electronics", Price = 1200 },
new Product { Name = "Keyboard", Category = "Electronics", Price = 75 },
new Product { Name = "Mouse", Category = "Electronics", Price = 25 },
new Product { Name = "Book", Category = "Books", Price = 15 },
new Product { Name = "Notebook", Category = "Books", Price = 10 }
};
var groupedProducts = products.GroupBy(p => p.Category)
.Select(g => new
{
CategoryName = g.Key,
ProductCount = g.Count(),
TotalPrice = g.Sum(p => p.Price),
Products = g.Select(p => new { p.Name, p.Price })
});
foreach (var group in groupedProducts)
{
Console.WriteLine($"Category: {group.CategoryName}");
Console.WriteLine($" Product Count: {group.ProductCount}");
Console.WriteLine($" Total Price: {group.TotalPrice}");
Console.WriteLine(" Products:");
foreach (var product in group.Products)
{
Console.WriteLine($" - {product.Name}: ${product.Price}");
}
Console.WriteLine();
}
}
}
Concepts Behind the Snippet
Real-Life Use Case
Consider a scenario where you have a list of sales transactions. You might want to group these transactions by customer, calculate the total revenue per customer, and list the products purchased by each customer. Anonymous types allow you to create a custom data structure to hold this information without defining a dedicated class for it.
Best Practices
Interview Tip
Be prepared to explain the difference between anonymous types and named types. Also, understand the benefits and drawbacks of using anonymous types in different scenarios. An interviewer might ask when you would choose an anonymous type over a class or struct.
When to use them
Anonymous types are ideal when you need to create a temporary data structure for a specific purpose within a limited scope (e.g., within a method). They are particularly useful when working with LINQ queries to project data into a custom format.
Memory footprint
Anonymous types are classes, so they are reference types. Each instance of an anonymous type allocates memory on the heap. However, the compiler optimizes the creation of anonymous types, so if you create multiple instances of the same anonymous type with the same properties, the compiler may reuse the same underlying class definition, potentially reducing memory overhead. Still, for large datasets or frequent creation of anonymous types, consider the memory implications and potentially opt for structs or named types if performance is critical.
Alternatives
Alternatives to using anonymous types include:
Pros
Cons
FAQ
-
Can I pass an anonymous type as an argument to a method?
Yes, you can pass an anonymous type as an argument to a method, but you'll need to use thedynamic
keyword or reflection to access its properties, or use generics to define the type if you know the structure at compile time. It's generally better to avoid this for public APIs. -
Are anonymous types classes or structs?
Anonymous types are classes (reference types). They are not structs (value types). -
Can I use anonymous types in async methods?
Yes, you can use anonymous types in async methods. They behave the same way as in synchronous methods.