C# 中的子查询获取 LINQ 查询中的最大值

发布于 2024-12-10 20:10:20 字数 1316 浏览 0 评论 0原文

我正在尝试创建一个子查询来从表中获取最新日期,与查询的其余部分无关。

我的查询如下。我想选择子表中的最高日期作为值并将其投影到下面的模型中。

我的其他标签是反馈,包含日期值和用户名字段

return (from t1 in db.TaskAppointmentOpens

                from t2 in db.Tasks.Where(t => (t.Task_ID == t1.Parent_Task_ID))

                from t3 in db.UserNames.Where(t => (t.User_Username == t2.OwnerTypeItem_ID))

                where ((t2.Item_ID > 0) && (t2.Type_ID > 0) && (t2.Creator == user) && (t1.AppointmentEnd < DateTime.Now) && (t1.AppointmentStart > EntityFunctions.AddMonths(DateTime.Now, -6)) && (from i in db.AppointmentFeedbacks where i.AppointmentId == t1.ID select i).Count() == 0)

                group new {t2, t3} by new { 
                  t2.OwnerTypeItem_ID, t3.Name
                } into g

                let oldestAppointment = g.Min(uh => uh.t2.Due_Date)

                    select new TelesalesNeglectedFeedbackModel
                    {
                        UserFullName = g.Key.Name,
                        QtyOutstanding = g.Select(x => x.t2.Task_ID).Distinct().Count(),
                        OldestAppointment = oldestAppointment
                        LastDateInOtherTable = HERE <======
                    }).Take(5).ToList();

I'm trying to create a sub-query to obtain the latest date from a table, unrelated to the rest of the query.

My query is below. I'd like to select the highest date in a sub-table as a value and project it to my model below.

My other tabe is Feedback and contains a date value and a username field

return (from t1 in db.TaskAppointmentOpens

                from t2 in db.Tasks.Where(t => (t.Task_ID == t1.Parent_Task_ID))

                from t3 in db.UserNames.Where(t => (t.User_Username == t2.OwnerTypeItem_ID))

                where ((t2.Item_ID > 0) && (t2.Type_ID > 0) && (t2.Creator == user) && (t1.AppointmentEnd < DateTime.Now) && (t1.AppointmentStart > EntityFunctions.AddMonths(DateTime.Now, -6)) && (from i in db.AppointmentFeedbacks where i.AppointmentId == t1.ID select i).Count() == 0)

                group new {t2, t3} by new { 
                  t2.OwnerTypeItem_ID, t3.Name
                } into g

                let oldestAppointment = g.Min(uh => uh.t2.Due_Date)

                    select new TelesalesNeglectedFeedbackModel
                    {
                        UserFullName = g.Key.Name,
                        QtyOutstanding = g.Select(x => x.t2.Task_ID).Distinct().Count(),
                        OldestAppointment = oldestAppointment
                        LastDateInOtherTable = HERE <======
                    }).Take(5).ToList();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2024-12-17 20:10:20

您应该能够通过这种方式访问​​表和字段:

LastDateInOtherTable = db.Feedback.Max(f => f.Date)

或者像对其他列所做的那样使用 let 子句,然后稍后对其进行分配:

let lastDate = db.Feedback.Max(f => f.Date)
...
LastDateInOtherTable = lastDate

You should be able to access the table and field this way:

LastDateInOtherTable = db.Feedback.Max(f => f.Date)

Or use a let clause as you've done for the other column and then assign it later:

let lastDate = db.Feedback.Max(f => f.Date)
...
LastDateInOtherTable = lastDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文