【发布时间】:2013-07-09 04:51:05
【问题描述】:
我通过加入从多个表中获取数据,并且我想对特定列值的数据进行分组,但在 group by 语句之后,我可以访问我的别名及其属性。我犯了什么错误?
public List<PatientHistory> GetPatientHistory(long prid)
{
using(var db = new bc_limsEntities())
{
List<PatientHistory> result =
(from r in db.dc_tresult
join t in db.dc_tp_test on r.testid equals t.TestId into x
from t in x.DefaultIfEmpty()
join a in db.dc_tp_attributes on r.attributeid equals a.AttributeId into y
from a in y.DefaultIfEmpty()
where r.prid == prid
group new {r,t,a} by new {r.testid} into g
select new PatientHistory
{
resultid = r.resultid,
bookingid = r.bookingid,
testid = r.testid,
prid = r.prid,
attributeid = r.attributeid,
result = r.result,
Test_Name = t.Test_Name,
Attribute_Name = a.Attribute_Name,
enteredon = r.enteredon,
Attribute_Type = a.Attribute_Type
}).ToList();
return result;
}
}
【问题讨论】:
-
不清楚您要做什么。分组后,序列的每个元素都是一个组 -
r不再作为范围变量存在,只有g,组......您完全忽略了它。您似乎误解了group子句的目的。 -
但我无法使用 g 变量访问属性
-
如何使用g变量访问列
-
好吧,
g是 group - 它是具有相同 testid 的所有记录,因为这就是您分组的依据...再次,真的不清楚是什么你正在努力实现 -
我需要根据 testid 对我选择的列进行分组