views:

30

answers:

2

How do I query using Ling to Entities.

I want to get back the total number of rows for a specific user.

I created a table with an ID(PK), Username, and PhoneNumber.

In my controller, I am able to do this and see my entities:

public ActionResult UserInfo()
        {
            using (UserInfoEntities db = new UserInfoEntities())
            {

            }
            return View();
        }

How do I query to return the total number of rows and declare it to a variable (totalrows) so that I can do thge following:

ViewData["TotalRows"] = totalrows

If there is a better way I am open to suggestions...

Thanks!!

+1  A: 

Probably something like:

int count = db.SomeTable.Where(r => r.Username.Equals("username")).Count();
Matthew Abbott
Exactly what I needed! Thanks
Mage
+1  A: 

Yep Matthew is right. The count method does exactly what you need! Although, I would recommend looking at ViewModels to pass your data to your view (rather than ViewData) and some kind of DAL pattern, such as the repository pattern, for querying your entities.

Bigfellahull
ViewModel? What do you mean? Putting this logic into my model?
Mage
Basically the ideal goal is to have a strongly typed view. By creating a separate class - or model - and passing that to the view, your view is strongly typed to that model. See here http://www.asp.net/mvc/videos/how-do-i-implement-view-models-to-manage-data-for-aspnet-mvc-views for a more complete explanation.
Bigfellahull