C# > Data Access > Database Access > LINQ to Entities
Simple LINQ to Entities Query
This snippet demonstrates a basic LINQ to Entities query to retrieve data from a database using Entity Framework Core.
Concepts Behind the Snippet
This snippet uses LINQ to Entities, which allows you to write LINQ queries that are translated into SQL and executed against a database. It leverages Entity Framework Core (EF Core), an ORM (Object-Relational Mapper), to map database tables to C# classes (entities).
Code Example
The code demonstrates retrieving a list of blogs from the database, filtering them based on the `Url` property (containing 'example'), ordering them by the `Title` property, and converting the results to a list. A DbContext is configured and used to interact with the database. In this case, an in-memory database is used. Make sure to configure appropriate database context and connectionstring in a real-world application.
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace DataAccessExample
{
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options) : base(options) { }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public string Title { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
// Replace with your actual connection string
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseInMemoryDatabase("TestDatabase"); //Or UseSqlServer for SQL Server
var options = optionsBuilder.Options;
using (var context = new BloggingContext(options))
{
// Add some sample data
context.Blogs.Add(new Blog { Url = "http://example.com/blog1", Title = "Blog 1" });
context.Blogs.Add(new Blog { Url = "http://example.com/blog2", Title = "Blog 2" });
context.SaveChanges();
// LINQ to Entities Query
var blogs = context.Blogs
.Where(b => b.Url.Contains("example"))
.OrderBy(b => b.Title)
.ToList();
// Output the results
foreach (var blog in blogs)
{
Console.WriteLine($"BlogId: {blog.BlogId}, URL: {blog.Url}, Title: {blog.Title}");
}
}
}
}
}
Explanation
1. DbContext and Entities: `BloggingContext` inherits from `DbContext` and represents the database context. `Blog` and `Post` are entity classes that map to database tables.
2. Database Configuration: The `DbContextOptionsBuilder` is used to configure the database connection (in this case, an in-memory database for simplicity). For production, you would use `UseSqlServer` (or other appropriate provider) and configure the connection string.
3. Adding Sample Data: The code adds two sample `Blog` entries to the database to demonstrate the query.
4. LINQ Query: The core of the snippet is the LINQ query: `context.Blogs.Where(b => b.Url.Contains("example")).OrderBy(b => b.Title).ToList();`
* `context.Blogs`: This accesses the `Blogs` DbSet, which represents the `Blogs` table.
* `.Where(b => b.Url.Contains("example"))`: This filters the blogs, selecting only those where the `Url` property contains the substring 'example'.
* `.OrderBy(b => b.Title)`: This sorts the filtered blogs alphabetically by their `Title` property.
* `.ToList()`: This executes the query and converts the results into a `List
Real-Life Use Case
Imagine a blogging application. This query could be used to retrieve a list of blogs that match a search term in their URL, ordered alphabetically for display on a search results page.
Best Practices
Interview Tip
Be prepared to explain the difference between LINQ to Objects and LINQ to Entities. LINQ to Objects operates on in-memory collections, while LINQ to Entities translates queries into SQL for execution against a database. Understanding the concept of 'materialization' (when the query is actually executed against the database) is also important.
When to Use Them
Use LINQ to Entities when you need to query data from a relational database in a type-safe and readable manner. It simplifies data access and reduces the amount of boilerplate code you need to write.
Memory Footprint
The memory footprint depends on the amount of data retrieved from the database. Using `.AsNoTracking()` can reduce the memory footprint for read-only queries. Be mindful of retrieving large datasets, as they can consume a significant amount of memory. Pagination and filtering can help to reduce the amount of data loaded into memory.
Alternatives
Alternatives to LINQ to Entities include:
Pros
Cons
FAQ
-
What is DbContext?
DbContext represents a session with the database and is used to query and save instances of your entities. -
What is DbSet?
DbSet represents a collection of entities of a specific type within the DbContext. -
What does `.ToList()` do in the LINQ query?
`.ToList()` executes the LINQ query against the database and materializes the results into a Listobject in memory.