A simple and straight forward C# LINQ code that you can use to retrieve and show the list of user's online with the username and last activity date from ASP.NET Membership data.
In my example below, I have a variable name called "dataContext" which is very descriptive, it is a DataContext that includes all my entities mapped over to my SQL Database.
ASP.NET Membership uses UTC time format so in order for me to get an accurate result, I used the UTC Current Datetime and minus it to the Membership's LastActivityDate.
Lastly, I show the results in a GridVew.
C# Code:
//get the data
var olst = from u in dataContext.aspnet_Users
where (DateTime.UtcNow - u.LastActivityDate).TotalMinutes <= 30
select new
{
UserId = u.UserId,
Username = u.UserName,
LastActivityDate = u.LastActivityDate.ToLocalTime()
};
//Show in GridView
gv.DataSource = olst.OrderByDescending(u => u.LastActivityDate);
gv.DataBind();
L.
ASP.NET Membership Show List of Users online
Lerrie | Wednesday, April 24, 2013 | Labels: C#, GridView, LINQ, Membership, Visual Studio
Subscribe to:
Posts (Atom)




