Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MyModel的linq查询_C#_Linq - Fatal编程技术网

C# 使用MyModel的linq查询

C# 使用MyModel的linq查询,c#,linq,C#,Linq,我有一个对象模型,我希望用linq查询填充它 MyModel{ DateTime AppointDate {get; set;} int TotalAppoints {get; set;} int AppointDuration {get; set;} } 我正在编写的查询返回特定日期的约会计数,并且位于一个函数中,该函数接收UserID和日期作为参数。MyDC是数据上下文 到目前为止,我的情况如下: var Output = from a in MyDC.AppointTable

我有一个对象模型,我希望用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 = ?
             };

目前,我已经尝试了一些方法,但没有达到预期效果。当我写一个.Count()时,我没有得到计数。每个约会都有一个int,用于存储该约会的分钟数;TotalAppoints应为约会计数,AppointDuration应为约会花费的所有分钟计数。感谢您的输入。

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

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

您没有显示您的数据模型,因此如何有人针对它编写linq查询?问题是我得到的是空的=“枚举未产生任何结果”,并且我从示例数据中知道它应该返回值。这意味着您的(来自上下文中的表Where…)与任何内容都不匹配,因此您没有可操作的数据on@frenchie-也许您可以提供示例数据?好的,where子句有问题。