user2790781
user2790781

Reputation: 85

Trying to count number of enteries in table

trying to count number of enteries in a database table. heres my code

var numberOfPeople = from rs in db.Results
                     select rs.E_Mail.ToList().Count().ToString();

Im trying to output this in a label using string. format. Its given me this error

Sequence operators not supported for type 'System.String'

Upvotes: 0

Views: 45

Answers (3)

Jonesopolis
Jonesopolis

Reputation: 25370

your whole query can be simplified to:

var numberofpeople = db.Results.Count.ToString();

you don't need the rest to simply count the rows.

Upvotes: 0

Alessandro D'Andria
Alessandro D'Andria

Reputation: 8878

You're applying ToList to E_Mail property. Just do that:

var numberOfPeople = from rs in db.Results
                     select rs.E_Mail;

var count = numberOfPeople.Count();

Upvotes: 1

Habib
Habib

Reputation: 223282

You are trying to convert each Email to List and then getting the count and calling ToString on it. You are missing brackets. Your call should be like:

var numberOfPeople = (from rs in db.Results
                     select rs.E_Mail).ToList().Count().ToString();

Or you can simplify it like:

var numberOfPeople = (from rs in db.Results
                     select rs.E_Mail).Count().ToString();

Since you are only interested in Count, there is no need to get all the records in memory. Also Count would return a numeric value, it is better if you store its numeric value and then convert it to a string if you need. like:

int numberOfPeople = (from rs in db.Results
                     select rs.E_Mail).Count();

Upvotes: 0

Related Questions