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

Posted on 2014-07-19
Last Modified: 2014-07-19
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

Question by:bmanmike39
    LVL 62

    Accepted Solution

    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


    Author Closing Comment

    Thank You!
    LVL 62

    Expert Comment

    by:Fernando Soto
    Not a problem bmanmike39, glad I could help.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now