5

I am getting data from multiple tables by joining and i want to group data on particular column value but after group by statement i can access my aliases and their properties. What mistake i am making?

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

1 回答 1

4

你这样做是错误的。正如 Jon 在使用别名r,对序列进行分组后所说的那样ta不存在。分组后,您会在 的每个元素中收到g具有rt、的序列的序列。如果您想从每个组中获取一个对象(例如最近的),您应该尝试以下操作:ag

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 = g.Select(x => x.r.resultid).Last(), // if you expect single value get it with Single()
         // .... here add the rest properties
         Attribute_Type = g.Select(x => x.a.Attribute_Type).Last()
     }).ToList();
于 2013-07-09T06:36:42.590 回答