我们如何对从数据库获取的数据执行groupby操作

问题描述 投票:0回答:2
string date = DateTime.Today.Date.ToShortDateString();
var grouped = from a in db.Logs
    group a by a.email
    into g
    select new
    {
        intime = (from x in db.Logs where x.date == date && x.email == "" select x.login).Min();
        outime = (from x in db.Logs where x.date == date && x.email == "" select x.login).Min()

    };
return View();

我正在使用有emaillogin_time的桌子。

我需要根据email对它们进行分组,并且我需要获得当前日期的特定login_time的最小email_id。我使用min函数找到第一次登录。我是LINQ的新手我的表有1.login 2.logout 3.email 4.username字段

每当用户登录时,表都会填充登录时间,注销时间,电子邮件,用户名。 =>我需要根据当前日期的电子邮件对这些详细信息进行分类。这样管理员视图页面应该只有当前日期的特定电子邮件的第一次登录和最后一次注销。

c# asp.net-mvc linq
2个回答
0
投票

你写了:

我需要根据电子邮件对它们进行分组,然后我需要获取当前日期的特定email_id的min login_time。

为此,我会使用Queryable.GroupBy with an ElementSelector

DateTime selectionDate = DateTime.UtcNow.Date;  // or any other date you want to use


var result = db.Logs
    // Keep only logs that have an e-mail on the selection date:
    .Where(log => log.LogInTime == selectionDate)

    // Group all remaining logs into logs with same email
    .GroupBy(log => log.email,

    // element selector: I only need the LoginTimes of the Logs
    log => log.LoginTime,

    // result selector: take the email, and all logInTimes of logs 
    // with this email to make a new object
    (email, logInTimesForThisEmail) =>  new
    {
        Email = email, // only if desired in your end result

        // Order the loginTimes and keep the first,
        // which is the min login time of this email on this date
        MinLoginTimeOnDate = logInTimesForThisEmail
            .OrderBy(logInTime => loginTime)
            .FirstOrDefault(),

您的示例代码显示您还需要所选日期的最长登录时间。使用单独的选择,因此您只需要对元素进行一次排序:

(email, logInTimesForThisEmail) =>  new
{
    Email = email, // only if desired in your end result
    LogInTimes = logInTimesForThisEmail
        .OrderBy(loginTime => loginTime);
})
.Select(groupResult => new
{
    Email = groupResult.Email,
    MinTime = groupResult.LogInTimes.FirstOrDefault(),
    MaxTime = groupResult.LogInTimes.LastOrDefault(),
});

如果您需要的字段多于Email和LoginTimes,请更改GroupBy的ElementSelector参数,以便它还包含其他字段。


0
投票
string date = DateTime.Today.Date.ToShortDateString();
var grouped = from a in db.Logs.ToList()
    where a.date == date
    group a by a.email
    into g
    let ordered = g.OrderBy(x => x.date).ToList()
    let firstLogin = ordered.First()
    let lastLogin = ordered.Last()
    select new
    {
        first_login_time = firstLogin.login_time,
        first_login = firstLogin.login,
        last_login_time = lastLogin.login_time,
        last_login = lastLogin.login        
    };

更新

管理员视图页面应该只有当前日期的特定电子邮件的第一次登录和最后一次注销。

string date = DateTime.Today.Date.ToShortDateString();
var grouped = from a in db.Logs.ToList()
    where a.login.Date == date
    group a by a.email
    into g
    let firstLogin = g.OrderBy(x => x.login).First() // order by login time and get first
    let lastLogout = g.OrderBy(x => x.logout).Last() // order by lotgout time and get last
    select new
    {
        email: g.Key,
        first_login = firstLogin.login, // first login
        last_logout = lastLogin.logout // last logout
    };

我希望你的loginlogout字段有datetime类型。你确实没有让问题更清楚。当我要求你获得表格模式时,我认为你让我这样做:

CREATE TABLE [dbo].[Logs](
    [username] [nvarchar](4000) NOT NULL,
    [email] [nvarchar](4000) NOT NULL,
    [login] [datetime] NULL,
    [logout] [datetime] NULL
)

或者也许是课堂宣言

public class Log {
  public string email {get; set; }
  public string username {get; set; }
  public DateTime login {get; set; }
  public DateTime logout {get; set; }

}

您可以学习如何提问

更新2

假设我有3个不同的电子邮件。如果他们访问我的应用程序,则会记录登录时间和登出时间。

Table[Login_details]
id  employee    date        login   logout  email
1   ShobaBTM    2019-03-18  16:12   16:12   [email protected]
2   neymarjr    2019-03-18  16:22   16:22   [email protected]
3   Cristiano   2019-03-18  16:23   16:23   [email protected]
4   neymarjr    2019-03-18  16:25   16:25   [email protected]
5   neymarjr    2019-03-18  16:30   16:32   [email protected]
6   neymarjr    2019-03-18  16:42   16:45   [email protected]

在管理员视图中我应该有这个

1   ShobaBTM    2019-03-18  16:12   16:12   
2   Cristiano   2019-03-18  16:23   16:23
3   neymarjr    2019-03-18  16:25   16:45   

好吧,试试这个:

string date = DateTime.Today.Date.ToShortDateString();
var grouped = from a in db.Logs.ToList()
    where a.date == date
    group a by new { a.employee, a.date }
    into g
    let firstLogin = g.OrderBy(x => TimeSpan.ParseExact(x.login, "hh\\:mm")).First() // order by login time and get first
    let lastLogout = g.OrderBy(x => TimeSpan.ParseExact(x.logout, "hh\\:mm")).Last() // order by logout time and get last
    select new
    {
        employee = g.Key.employee,
        date = g.Key.date,
        first_login = firstLogin.login, // first login
        last_logout = lastLogin.logout // last logout
    };

这段代码有用吗?如果不是 - 究竟发生了什么?

© www.soinside.com 2019 - 2024. All rights reserved.