将复杂对象与嵌套集合放在一起

时间:2017-12-29 01:12:01

标签: c# sql sql-server linq linq-to-sql

表保存有关数据加载的信息。它的加载是我重命名为批处理(可能不是一个完美的名称)。步骤和集合是已经定义的元表,它们的内容将来可能不会更新(至少没有旧的内容会被删除)。集合是数据类型的定义,例如“具有此或那个的csv文件”。几组都迈出了一步。几个步骤批量生产。一个集合可以绑定到多个步骤,因此可以重用该集合。实际(已加载)数据存储在其他表中,但对于此特定任务并不重要。

我有这个(简化的)DB结构: enter image description here

(日志表没有外键约束。)

我需要从中获得这样的东西:

public class JoinedModel
{
    public Batch Batch { get; set; }
    public IEnumerable<Step> Steps { get; set; }
}

public class Step
{
    public int StepId { get; set; }
    public string Description { get; set; }
    public IEnumerable<Set> Sets { get; set; }
}

public class Set
{
    public int SetId { get; set; }
    public string Type { get; set; }
    public string EventCode { get; set; }
}

如何将单个Batch与一组Step个对象连接起来,每个对象都有一组适当的Set个对象(注意输出集类有事件代码,从{{获得) 1}}和Logs表)?

尝试了多个'来自',加入,分组,但我找不到解决方案。是否有可能从一个陈述中做出类似的事情,或者我是否必须单独选择Events然后接下来呢?那怎么样?

无法更改数据库设计。

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Logs> logs = new List<Logs>();
            List<Step> steps = new List<Step>();
            List<Set> sets = new List<Set>();
            List<Batch> batches = new List<Batch>();
            List<Event> events = new List<Event>(); 


            int BatchId = 123;

            var results = (from log in logs
                           join step in steps on log.Steps_StepId equals step.StepId
                           join set in sets on log.Sets_SetId equals set.SetId
                           join batch in batches on log.Batch_BatchId equals batch.BatchId
                           join _event in events on log.Events_EventId equals _event.EventId
                           select new { log = log, step = step, set = set, batch = batch, _event = _event })
                           .Where(x => x.batch.BatchId == BatchId)
                           .Select(x => new {
                               description = x.batch.Description,
                               eventCode = x._event.Code,
                               date = x.log.Date,
                               stepId = x.step.StepId,
                               stepDescription = x.step.Description,
                               setType = x.set.SetId
                           }).ToList();


        }

    }
    public class Logs
    {
        public int Batch_BatchId { get; set; }
        public int Steps_StepId { get; set; }
        public int Sets_SetId { get; set; }
        public DateTime Date { get; set; }
        public int Events_EventId { get; set; }

    }
    public class JoinedModel
    {
        public Batch batch { get; set; }
        public List<Step> Steps { get; set; }
    }

    public class Step
    {
        public int StepId { get; set; }
        public string Description { get; set; }
        public List<Set> Sets { get; set; }
    }

    public class Set
    {
        public int SetId { get; set; }
        public string Type { get; set; }
        public string EventCode { get; set; }
    }
    public class Batch
    {
        public int BatchId { get; set; }
        public string Description { get; set; }
    }
    public class Event
    {
        public int EventId { get; set; }
        public int Code { get; set; }
    }
}