查看: 2315|回复: 26
|
Attendance Management System 的疑问 (紧急)
[复制链接]
|
|
我能够将一个list的学生和status(absent/present)加进database
database是visual studio 的 local sql database server (dbo.StudentAttendance)
database 里面 一个studentID 可以 有很多其他的 entry
example:
studentID classID Status
WAR0001 CLS0001 Present
WAR0001 CLS0020 Absent
WAR0001 CLS0009 Present
我的疑问是:
要怎样算student WAR0001 total Present 几次, Absent 几次?
|
|
|
|
|
|
|
|
发表于 1-8-2014 04:40 PM
|
显示全部楼层
group by classID where studentID = WAR0001 and Status = Present |
|
|
|
|
|
|
|
发表于 1-8-2014 04:40 PM
|
显示全部楼层
簡短的作法:- select status,count(status) from
- (select * from students where studentid='war0001') s
- group by status
复制代码 |
|
|
|
|
|
|
|
发表于 1-8-2014 04:45 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 1-8-2014 04:51 PM
|
显示全部楼层
xtjj 发表于 1-8-2014 04:45 PM 
sample 在這
http://sqlfiddle.com/#!2/29f99/4
你好,谢谢你的回答
请问你,
如果我要全部的student 的 absent count 能不能做到?
例如:
insert into students values('war0001', 'cls0001', 'present');
insert into students values('war0001', 'cls0009', 'absent');
insert into students values('war0002', 'cls0009', 'absent');
insert into students values('war0002', 'cls0011', 'absent');
insert into students values('war0002', 'cls0014', 'present');
我需要
WAR0001 absent count = 1
WAR0002 absent count = 2
如果用LINQ 能做到吗?
|
|
|
|
|
|
|
|
发表于 1-8-2014 05:04 PM
|
显示全部楼层
- List<Student> students = new List<Student>();
-
- var sub = from p in students
- where p.Status == "absent"
- select new { p.StudentID, p.Status };
-
- var absence = from p in sub
- group p by p.StudentID into k
- select new { ID = k.Key, Count = k.Count() };
-
- foreach (var p in absence)
- {
- Console.WriteLine("{0} absent count = {1}", p.ID, p.Count);
- }
复制代码 |
|
|
|
|
|
|
|
发表于 1-8-2014 05:10 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 1-8-2014 05:11 PM
|
显示全部楼层
xtjj 发表于 1-8-2014 05:04 PM 
这位大大,
请问 VB LINQ 做得到吗?
我需要LINQ statement 来 set DataGridView 的
datasource
谢谢。。 感激不尽
|
|
|
|
|
|
|
|

楼主 |
发表于 1-8-2014 05:12 PM
|
显示全部楼层
xtjj 发表于 1-8-2014 05:10 PM 
同學?
http://cforum.cari.com.my/forum.php?mod=viewthread&tid=3465140#lastpost
是的,是groupmate。 haha
|
|
|
|
|
|
|
|
发表于 1-8-2014 05:21 PM
|
显示全部楼层
- Dim sss = From p In students Where p.Status = "absent" New From { _
- p.StudentID, _
- p.Status _
- }
- Dim absence = From k In From p In [sub]Group p By p.StudentID New With { _
- Key .ID = k.Key, _
- Key .Count = k.Count() _
- }
复制代码 dgv1.Datasource = absence |
|
|
|
|
|
|
|
发表于 1-8-2014 05:52 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 1-8-2014 06:10 PM
|
显示全部楼层
try- Dim sss = From p In students Where p.Status = "absent" Select New With {p.StudentID, p.Status}
- Dim absence = From p in sss Group By StudentID = p.StudentID Into g = Group Select StudentID, g.Count()
复制代码 |
|
|
|
|
|
|
|
发表于 1-8-2014 06:20 PM
|
显示全部楼层
这次没有syntax error 但是 datagridview还是显示 duplicate data |
|
|
|
|
|
|
|
发表于 1-8-2014 06:29 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 1-8-2014 06:34 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 1-8-2014 06:45 PM
|
显示全部楼层
- Dim students as new List(of Student)() From {
- New Student with {.StudentID = "war0001", .ClassID="cls0009", .Status="present"}, _
- New Student with {.StudentID = "war0001", .ClassID="cls0001", .Status="absent"}, _
- New Student with {.StudentID = "war0002", .ClassID="cls0009", .Status="absent"}, _
- New Student with {.StudentID = "war0002", .ClassID="cls0011", .Status="absent"}, _
- New Student with {.StudentID = "war0002", .ClassID="cls0014", .Status="present"} _
- }
- Dim sss = From p In students Where p.Status = "absent" Select New With {p.StudentID, p.Status}
- Dim absence = From p in sss Group By StudentID = p.StudentID Into g = Group Select StudentID, g.Count()
复制代码 |
|
|
|
|
|
|
|
发表于 1-8-2014 06:46 PM
|
显示全部楼层
我們老師按照你的資料, 和一樣的 code, 答案如上. |
|
|
|
|
|
|
|
发表于 1-8-2014 06:55 PM
|
显示全部楼层
我的结果是这样
|
|
|
|
|
|
|
|
发表于 1-8-2014 07:13 PM
|
显示全部楼层
你把 solution 和 database 上傳, 然後我叫老師下載了幫你看下. |
|
|
|
|
|
|
|
发表于 1-8-2014 07:19 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|