Entity Framework Core でレコード数を取得するコードを紹介します。
概要
レコード数を取得する場合は、
Count()
メソッドを利用します。
書式
(クエリ構文、または、メソッド構文のLINQ).Count();
プログラム
事前準備:テーブルの作成
以下のテーブルを用意します。
ProductsB テーブルid | model | name | class | category | price |
1 | C-XM01 | モーダンチェア | ホーム | チェア | 56000 |
2 | X-XD05 | ラージデスク | オフィス | テーブル | 87000 |
3 | A-DA40 | ラウンドダイニングチェア | ホーム | チェア | 28000 |
4 | O-XX100 | ナチュラルオフィス | オフィス | チェア | 13800 |
5 | R-D400 | ラウンドダイニングテーブル | ホーム | テーブル | 128000 |
6 | R7000 | ウッドキャビネット | オフィス | その他 | 32000 |
7 | B-200 | リネンベッド | ホーム | ベッド | 184500 |
8 | B-250 | ホワイトダブルベッド | ホーム | ベッド | 324850 |
9 | W-80 | ワーキングチェア | オフィス | チェア | 45000 |
10 | EG-10X | エルゴノミクスデスク | オフィス | テーブル | 88500 |
11 | NC-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