[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

How do you get the count of records in a db with Entity 4

I'm trying to get the count of the total number of  departments in my db.  but I don't know how to do this with entity. below is what I have and I'm stuck.

Code for button click event:
OrgEntities OE = new OrgEntities();
 tbl_Dept d = OE.tbl_Dept.Count()

Lbl_deptCount.text = d.ToString();

Open in new window


Sample of what I'm Trying to do with Entity:

SELECT *  FROM tbl_Dept  WHERE Did = 2

Open in new window

0
bmanmike39
Asked:
bmanmike39
  • 2
1 Solution
 
Fernando SotoCommented:
Hi bmanmike39;

This should give you the count of all records in the tbl_Dept table. You almost had it, you just needed to change the data type to int because the Count() method returns an integer.

// Assuming that OrgEntities is DbContext
OrgEntities OE = new OrgEntities();
int d = OE.tbl_Dept.Count();
Lbl_deptCount.text = d.ToString();

Open in new window

0
 
bmanmike39Author Commented:
Thank You!
0
 
Fernando SotoCommented:
Not a problem bmanmike39, glad I could help.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now