C# 中的子查询获取 LINQ 查询中的最大值
我正在尝试创建一个子查询来从表中获取最新日期,与查询的其余部分无关。
我的查询如下。我想选择子表中的最高日期作为值并将其投影到下面的模型中。
我的其他标签是反馈,包含日期值和用户名字段
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够通过这种方式访问表和字段:
或者像对其他列所做的那样使用
let
子句,然后稍后对其进行分配:You should be able to access the table and field this way:
Or use a
let
clause as you've done for the other column and then assign it later: