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

问题描述 投票:-2回答:1

表保存有关数据加载的信息。它的加载是我重命名为批处理(可能不是一个完美的名称)。步骤和集合是已经定义的元表,它们的内容将来可能不会更新(至少没有旧的内容会被删除)。集合是数据类型的定义,例如“具有此或那个的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对象(注意输出集类有事件代码,从LogsEvents表中获得)?

尝试了多个'从',加入,分组,但我没有那么好的解决方案。是否有可能从一个声明中做出类似的事情,或者我必须分别选择Batch然后接下来呢?那么如何?

数据库设计无法更改。

c# sql sql-server linq linq-to-sql
1个回答
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; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.