Linq在GroupBy选择中包含额外列

时间:2017-02-25 22:33:18

标签: c# sql linq

我有一个“ReactionSimple”列,我想在GroupBy选择中包含但是当我将它包含在Group By中时,它会大大抛弃返回的行数。有没有办法在最终分组选择中包含注释掉的列而不会丢弃结果计数。我是LINQ菜鸟。

        var reactivityResults = new List<ReactivityResultViewModel>();
        var reactivityResults2 = new List<ReactivityResultViewModel>();
        var classifications = GetMaterialClassifications(test.TestType);
        var antigens = _db.Antigens.ToList();
        if (test.ResultSets.Any())
        {
            var testResult = test.ResultSets.FirstOrDefault();
            var dateReported = testResult.DateReported;
            var details = testResult.ResultSetDetails.ToList();
            if (details.Any())
            {
                reactivityResults = (from d in details
                                     join a in _db.Antigens on d.AntigenId equals a.AntigenId
                                     join ma in _db.MaterialAntigens on a.AntigenId equals ma.AntigenId
                                     join md in _db.MaterialDefs on ma.MaterialDefId equals md.MaterialDefId
                                     join c in classifications on md.MaterialId equals c.MaterialId
                                     select new ReactivityResultViewModel
                                     {
                                         MaterialId = c.MaterialId,
                                         MaterialName = c.MaterialName,
                                         IsActive = md.IsActive,
                                         ReactionSimple = (ReactionTypeEnum)d.ReactionSimple,
                                         TestId = test.TestId,
                                         AccessionId = test.AccessionId,
                                         PatientFirstName = test.Patient.Party.NameFirst,
                                         PatientLastName = test.Patient.Party.NameLast,
                                         TestType = (TestTypeNoFlagsEnum)test.TestType,
                                         DateReported = dateReported,
                                         ManufacturerId = c.ManufacturerId,
                                         ManufacturerName = c.ManufacturerName
                                     })
                                     .OrderBy(x => x.MaterialName)
                                     .ToList();

                var sreactivityResults2 = reactivityResults
                    .GroupBy(x => new {
                        x.MaterialId, x.AccessionId, x.DateReported,
                        x.IsActive, x.ManufacturerId, x.ManufacturerName,
                        x.MaterialName, x.PatientFirstName, x.PatientLastName,
                        x.TestId, x.TestType,
                        //x.ReactionSimple
                    })
                    .Select(x => new ReactivityResultViewModel
                    {
                        MaterialId = x.Key.MaterialId,
                        MaterialName = x.Key.MaterialName,
                        IsActive = x.Key.IsActive,
                        //ReactionSimple = x.Key.ReactionSimple,
                        TestId = test.TestId,
                        AccessionId = test.AccessionId,
                        PatientFirstName = test.Patient.Party.NameFirst,
                        PatientLastName = test.Patient.Party.NameLast,
                        TestType = (TestTypeNoFlagsEnum)test.TestType,
                        DateReported = dateReported,
                        ManufacturerId = x.Key.ManufacturerId,
                        ManufacturerName = x.Key.ManufacturerName
                    })
                    .ToList();

                var sd2 = sreactivityResults2.ToList();

            }

            return reactivityResults;

0 个答案:

没有答案