基本查詢Basic Queries

了解如何使用語言整合 Query (LINQ) 從資料庫載入實體。Learn how to load entities from the database using Language Integrate Query (LINQ).

提示

您可以檢視這篇文章範例GitHub 上。You can view this article's sample on GitHub.

101 個 LINQ 範例101 LINQ samples

此頁面會顯示一些可達到使用 Entity Framework Core 的一般工作的範例。This page shows a few examples to achieve common tasks with Entity Framework Core. 一組廣泛的範例顯示 LINQ 極限,請參閱101 個 LINQ 範例For an extensive set of samples showing what is possible with LINQ, see 101 LINQ Samples.

載入所有的資料Loading all data

using (var context = new BloggingContext())
{
    var blogs = context.Blogs.ToList();
}

載入單一實體Loading a single entity

using (var context = new BloggingContext())
{
    var blog = context.Blogs
        .Single(b => b.BlogId == 1);
}

篩選Filtering

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Where(b => b.Url.Contains("dotnet"))
        .ToList();
}