1

假设我有一个非常简单的数据库图:

在此处输入图像描述

以及对它的看法:

 create view vTraining
as
select t.Id as TrainingId,t.[Status]  , 
t.[User], t.Title,t.Organisation,t.[Month],t.[Year], 
s.Id as SubjectId, s.Name as SubjectName,
c.Text as Comment
from Training t
join Subject s on s.Training = t.Id
join Comment c on c.Training = t.Id

带有样本数据:

在此处输入图像描述

如您所见,这是一个包含三个主题的单一培训。我想通过 linq to sql 将此结果映射到此结构:

public class ViewModel
    {
        public string Comment { set; get; }
        public List<Item> Trainings { set; get; }
    }

    public class Item
    {
        public int TrainingId { set; get; }
        public int User { set; get; }
        public int Status { set; get; }
        public string Title { set; get; }
        public string Organisation { set; get; }
        public int? Month { set; get; }
        public int Year { set; get; }
        public List<KeyValuePair<int, string>> Subjects { set; get; }
    }

这是我创建的查询:

 var data = (from training in dc.vTrainings
                            group training by new
                           {
                               training.TrainingId,
                               training.Status,
                               training.Month,
                               training.Organisation,
                               training.Title
                           }
                                into g
                                select new ViewModel()
                                {
                                    Comment = g.Select(x =>
                                    x.Comment).First(),
                                    Trainings = g.Select(
                                     x => new Item()
                                     {
                                         Month = x.Month,
                                         Organisation = x.Organisation,
                                         Title = x.Title,
                                         Year = x.Year,
                                         Subjects = g.Select(
                                         z => new KeyValuePair<int, string>(z.SubjectId, z.SubjectName)).ToList()
                                     }).ToList()

                                })//.GroupBy(x => x.Trainings).Select(x => x.Key)
                                    .ToList();

不幸的是,我得到的结果不是我想要的: 在此处输入图像描述

ViewModel 对象只创建可以的对象,但对于每个单独的主题,都会创建新的项目(应该是一个)。Subjets 列表已正确创建。我试图创建第二组,以及其他一些东西,但这是我现在能得到的最好结果。如何编写此查询以获取一个 ViewModel 对象,该对象具有一个 Item 对象和三个主题?

4

2 回答 2

1

我终于得到了正确的结果:

var data = (from g in
                                (from training in dc.vTrainings
                                 where training.Status ==1
                                 group training
                                 by new
                                {
                                    training.TrainingId,
                                    training.Status,
                                    training.Month,
                                    training.Organisation,
                                    training.Title
                                }
                                     into g
                                     select g).AsEnumerable()

                            select new ViewModel()
                            {
                                Comment = g.Select(x =>
                                x.Comment).FirstOrDefault(),
                                Trainings = g.GroupBy(x => x.Status).Select(
                                 x => new Item()
                                 {
                                     Month = g.Key.Month,
                                     Organisation = g.Key.Organisation,
                                     Title = g.Key.Title,
                                     Subjects = (from i in g select new KeyValuePair<int, string>(i.SubjectId, i.SubjectName)).ToList()
                                 }).ToList()
                            }).ToList();

此查询仅适用于一次培训,但对我来说这不是问题,因为我按最新状态对其进行了过滤。我仍然很好奇如何为不止一次培训编写它。

于 2012-08-07T07:42:37.770 回答
1

尝试这个:

            var trainings =
                dc.vTrainings
                    .GroupBy(
                        t => new
                        {
                            t.TrainingId,
                            t.Status,
                            t.Month,
                            t.Organisation,
                            t.Title,
                            t.User,
                            t.Year
                        },
                        t =>
                        new
                        {
                            t.SubjectId,
                            t.SubjectName
                        })
                    .ToList()
                    .Select(
                        t =>
                            new Item
                            {
                                TrainingId = t.Key.TrainingId,
                                Status = t.Key.Status,
                                Month = t.Key.Month,
                                Organisation = t.Key.Organisation,
                                Title = t.Key.Title,
                                User = t.Key.User,
                                Year = t.Key.Year,
                                Subjects =
                                    t.Select(s => new KeyValuePair<int,string>(s.SubjectId,s.SubjectName)).ToList()
                            });
于 2012-08-07T07:19:16.547 回答