【发布时间】:2015-12-06 05:53:17
【问题描述】:
我的 linq 查询有问题。
我的查询是这样的:
var results = db.rms_section_vw.Where(s => s.sect_activeflag == true &&
s.dept_isactive == true)
.GroupBy(s => s.sect_id)
.Select(r => r.FirstOrDefault()).ToList();
但仍然在显示记录,有重复。
我的桌子在视图中:
Dept ID Sect ID Dept name Sect Name dept_isactive sect_isactive
1 1 Dept 1 Sect 1 1 1
1 2 Dept 1 Sect 2 1 1
何时显示结果:
Dept name Sect name
Dept 1 Sect 1
Dept 1 Sect 1 (supposed to be Sect 2)
Dept 2 Sect 3
Dept 2 Sect 3 (supposed to be Sect 4)
我的查询有什么问题?
【问题讨论】:
-
您得到的结果是正确的,因为您使用了 "SectId" {1, 2} 的 GroupBy 如果“SectId”相同,那么您将获得预期的结果。
-
@Chandru,我忘了,我已经更新了我的问题,具体是显示的结果。当我尝试使用查询时,结果就是这样。
-
你能否在表格视图中添加更多记录并添加预期的输出,因为它不是更清楚。
-
@Chandru,查看已编辑的问题
-
根据您的表格,您有 {SectId : 1 , DeptName : Dept1, SectName : Sect1 } 和 {SectId : 2 , DeptName : Dept1, SectName : Sect2 },如果您尝试使用此数据,您将得到此 {"Dept1" , "Sect 1"} 和 {"Dept1"," Sect2"} 但根据您的输出 {"Dept1" , "Sect 1"} 和 {"Dept1","Sect1"} 所以它不是重复的,这意味着数据重复?我是对的还是你的输出数据错了?