开发者

linq query with MyModel

开发者 https://www.devze.com 2023-02-19 20:01 出处:网络
I have an object model and I\'m looking to populate it with a linq query. MyModel{ DateTime AppointDate {get; set;}

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.App开发者_运维知识库ointTable
             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.


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 )
            };
0

精彩评论

暂无评论...
验证码 换一张
取 消