62,272
社区成员
发帖
与我相关
我的任务
分享
| 学生编号 | 姓名 |
| 001 | 张三 |
| 002 | 李四 |
| 编号 | 学生编号 | 科目 | 成绩 |
| 1 | 001 | 语文 | 90 |
| 2 | 002 | 语文 | B |
| 3 | 001 | 数学 | 95 |
| 4 | 003 | 化学 | C |
| 5 | 001 | 体育 | 85 |
我想得到的结果是 一张表比如其中的一行是 001,张三,3(这个是成绩表相关的行数有三行),270(这个是总分)
这两个EF模板要怎么设计呢?
用linq表达可以写成这样,把其中storeins和storeouts两个表名更换即可:
from storeIns in db.storeIns
select new {
storeIns.InStoreId,
all = (int?)
(from storeOuts in db.storeOuts
where
storeOuts.InId == storeIns.InStoreId
select new {
storeOuts.OutQuantity
}).Sum(p => p.OutQuantity),
counts =
(from storeOuts in db.storeOuts
where
storeOuts.InId == storeIns.InStoreId
select new {
storeOuts.OutQuantity
}).Count(p => p.OutQuantity != null)
}
SQL语句可以直接查询结果
用sql语句很容易就可以达到要求得到一个结果,比如:select * , count(select * from b) as 成绩行数,(select sum(b.result) where b.studentsid=a.studnetid) from students a,studentsResult b
现在这
现在这两个表的模型我设计成这样,但是肯定是达不到要求
public class Students
{
public int StudentsId { get; set; }
public string? StudentName { get; set;}
public ICollection<StudentsResult> studentsResults { get; set; }
}
public class StudentsResult
{
public int Id { get; set; }
public string? Course { get; set; }
public int Result { get; set; }
public Students students { get; set; }
}