使用 MyModel 进行 linq 查询

发布于 2024-10-26 07:58:42 字数 720 浏览 1 评论 0原文

我有一个对象模型,我希望用 linq 查询填充它。

MyModel{
DateTime AppointDate {get; set;}
int TotalAppoints {get; set;}
int AppointDuration {get; set;}
}

我正在编写的查询返回特定日期的约会计数,并且位于接收 UserID 和日期作为参数的函数中。 MyDC 是数据上下文。

到目前为止,这就是我所拥有的:

var Output = from a in MyDC.AppointTable
             where a.UserID == TheUserID
             where a.Date == TheDate.Date
             select new MyModel
             {
                AppointDate = ?,

                TotalAppoints =?,

                AppointDuration = ?
             };

现在,我已经尝试了一些方法,但它没有返回预期的结果。当我写 a.Count() 时,我没有得到计数。每个约会都有一个 int 来存储该约会的分钟数; TotalAppoints 应该是约会的计数,AppointDuration 应该保存约会所花费的所有分钟数。感谢您的意见。

I have an object model and I'm looking to populate it with a linq query.

MyModel{
DateTime AppointDate {get; set;}
int TotalAppoints {get; set;}
int AppointDuration {get; set;}
}

The query I'm writing is returning the count of appointments for a specific date and is in a function that receives the UserID and the date as parameters. MyDC is the data context.

So far, this is what I have:

var Output = from a in MyDC.AppointTable
             where a.UserID == TheUserID
             where a.Date == TheDate.Date
             select new MyModel
             {
                AppointDate = ?,

                TotalAppoints =?,

                AppointDuration = ?
             };

For now, I've tried a few things but it's not returning what's expected. When I write a.Count() I'm not getting the count. Each appointment has an int that stores the number of minutes for that appointment; TotalAppoints is supposed to be the count of appointments and AppointDuration is supposed to hold the count of all the minutes spent in appointments. Thanks for your input.

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

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

发布评论

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

评论(1

寻梦旅人 2024-11-02 07:58:42

您想要做的是将数据按该字段分组并对值进行求和

var out = from a in MyDC.AppointTable
            where a.UserID == TheUserID
            where a.Date == TheDate.Date
            group a by a.Date into g
            select new MyModel {
               AppointDate = TheDate.Date,
               TotalAppoints = g.Count(),
               AppointDuration = g.sum( x => x.AppointDuration )
            };

What you want to do is group your data by that field and sum across the values

var out = from a in MyDC.AppointTable
            where a.UserID == TheUserID
            where a.Date == TheDate.Date
            group a by a.Date into g
            select new MyModel {
               AppointDate = TheDate.Date,
               TotalAppoints = g.Count(),
               AppointDuration = g.sum( x => x.AppointDuration )
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文