0

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!!

2 Answers 2

2

Probably something like:

int count = db.SomeTable.Where(r => r.Username.Equals("username")).Count();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

ViewModel? What do you mean? Putting this logic into my model?
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 asp.net/mvc/videos/… for a more complete explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.