Entity Framework Core でレコード数を取得する - C#

Entity Framework Core でレコード数を取得するコードを紹介します。

概要

レコード数を取得する場合は、Count()メソッドを利用します。

書式

(クエリ構文、または、メソッド構文のLINQ).Count();

プログラム

事前準備:テーブルの作成

以下のテーブルを用意します。
ProductsB テーブル
idmodelnameclasscategoryprice
1C-XM01モーダンチェアホームチェア56000
2X-XD05ラージデスクオフィステーブル87000
3A-DA40ラウンドダイニングチェアホームチェア28000
4O-XX100ナチュラルオフィスオフィスチェア13800
5R-D400ラウンドダイニングテーブルホームテーブル128000
6R7000ウッドキャビネットオフィスその他32000
7B-200リネンベッドホームベッド184500
8B-250ホワイトダブルベッドホームベッド324850
9W-80ワーキングチェアオフィスチェア45000
10EG-10Xエルゴノミクスデスクオフィステーブル88500
11NC-208ナチュラルウッドチェアホームチェア128000

UI

下図のフォームを作成します。
今回は、[Count]と[Where + Count]のbutton7, button8 のみを利用します。

コード

以下のコードを記述します。
namespace SimpleEntityFrameworkCoreSqlServer
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button7_Click(object sender, EventArgs e)
    {
      IPentecSandBoxContext cx = new IPentecSandBoxContext();
      int result = cx.ProductsBs.Count();
      //int result = (from t in cx.ProductsBs select t).Count(); //クエリ構文の場合
      textBox1.Text += string.Format("レコード数: {0:d}", result);
    }

    private void button8_Click(object sender, EventArgs e)
    {
      IPentecSandBoxContext cx = new IPentecSandBoxContext();
      int result = cx.ProductsBs.Where(r => r.Category == "チェア").Count();
      //int result = (from t in cx.ProductsBs where t.Category == "チェア" select t).Count(); //クエリ構文の場合
      textBox1.Text += string.Format("レコード数: {0:d}", result);
    }
  }
}

解説

テーブルのレコード数を取得する場合

テーブルのレコード数を取得する場合のコードは以下になります。
取得したいレコード数のテーブルのDbSetオブジェクトのCount()メソッドを呼び出すとテーブルのレコード数を取得できます。
    private void button7_Click(object sender, EventArgs e)
    {
      IPentecSandBoxContext cx = new IPentecSandBoxContext();
      int result = cx.ProductsBs.Count();
      textBox1.Text += string.Format("レコード数: {0:d}", result);
    }

クエリ構文の場合も同様です。クエリ構文のLINQに対して、Count()メソッドを呼び出しレコード数を取得します。
    private void button7_Click(object sender, EventArgs e)
    {
      IPentecSandBoxContext cx = new IPentecSandBoxContext();
      int result = (from t in cx.ProductsBs select t).Count(); 
      textBox1.Text += string.Format("レコード数: {0:d}", result);
    }

検索結果のレコード数を取得する場合

検索結果のレコード数を取得する場合のコードです。
メソッド構文の場合は、Where()メソッドの戻り値に対して、Count()メソッドを呼び出し、検索結果のレコード数を取得します。
    private void button8_Click(object sender, EventArgs e)
    {
      IPentecSandBoxContext cx = new IPentecSandBoxContext();
      int result = cx.ProductsBs.Where(r => r.Category == "チェア").Count();
      textBox1.Text += string.Format("レコード数: {0:d}", result);
    }

クエリ構文の場合は、クエリ構文のLINQに対して、Count()メソッドを呼び出しレコード数を取得します。
    private void button8_Click(object sender, EventArgs e)
    {
      IPentecSandBoxContext cx = new IPentecSandBoxContext();
      int result = (from t in cx.ProductsBs where t.Category == "チェア" select t).Count(); //クエリ構文の場合
      textBox1.Text += string.Format("レコード数: {0:d}", result);
    }

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


[Count]のボタンをクリックします。テーブルのレコード数がテキストボックスに表示されます。


[Where + Count]ボタンをクリックします。Categoryが"チェア"であるレコードの数が、テキストボックスに表示され、検索結果のレコードを数を取得できています。


Entity Framework Core でレコード数の取得ができました。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2023-07-02
作成日: 2023-07-02
iPentec all rights reserverd.