Mona Karimi
Mona Karimi

Reputation: 63

How to get first username in database with linq?

I want get first username that register in my site.
I have following code:

Database1Entities db = new Database1Entities();
string username = db.tblMembers.Min().Select(x => x.username);

But not working.

I try this:

string username = db.tblMembers.Min(x=>x.CreateTime).Select(x => x.username);

but not work.

Upvotes: 1

Views: 109

Answers (2)

zey
zey

Reputation: 6105

You have to create CreatedTime or InsertedTime in your Database table if you don't have it ! And you can order by this time field , and get the top value ,

Database1Entities db = new Database1Entities();
string username =  
db.tblMembers.OrderBy(n => n.CreatedTime.Value).Select(x => x.username).Take(1);

Upvotes: 3

Aheho
Aheho

Reputation: 12821

Database1Entities db = new Database1Entities();
var YourFirstUser = db.tblMembers.OrderBy( u => u.CreatedTime).Select( u => u.UserName).FirstOrDefault();

Upvotes: 0

Related Questions