LINQ nested (inner) JOIN does not join children

对着背影说爱祢 提交于 2021-02-18 19:42:30

问题


Being rather new to LINQ, I am probably not looking for the correct search terms, that's why I am asking here.

I have a data transfer object (DTO) for the current timestep and a DTO for the previous timestep. I am intending to JOIN this information into a single (nested) list and display procentual changes (for all entries of the list). The nested contains nodes of type myObject. In other words, the list consists of a couple of parent entries and (different levels of) child elements of the same type. The merging step towards dataFullDto works perfectly for the parent entries only, for the child entries, the destination object does not contain the information of the previous timestep, PreviousValue and DeltaPercentage are null. Is it possible to perform that action on all nodes of a nested list without a foreach loop?

Assume that there is a function inputData(int timestep) which gives back all the required data. I inherited the following code which contains a LINQ inner JOIN:

var dataCurrentDto = inputData(time).GroupBy(x => x.Position) // Position just contains .Id and .Name
    .Select(x => new myObject {
       PositionId = x.Key?.Id,
       PositionName = x.Key?.Name,
       children = x.Select(Mapper.Map<myObjectDto>),
       Value = x.Sum(y => y.Value),
       PreviousValue = 0, // just to initialize, to be overwritten asap
       DeltaPercentage = 0, // dito
    });
var dataPreviousDto = inputData.Select(time-1).GroupBy(x => x.Position)
    .Select(x => new myObject {
        PositionId = x.Key?.Id,   
        PositionName = x.Key?.Name,
        children = x.Select(Mapper.Map<myObjectDto>),
        Value = x.Sum(y => y.Value),
    });   
dataFullDto = from current in dataCurrentDto join previous in dataPrevious on
              new { current.PositionId, current.SubPositionId }
              equals new { previous.PositionId, previous.SubPositionId }
              select new myObjectDto {
                 PositionId = current.PositionId,
                 PositionName = current.PositionName,
                 children = current.children,
                 SubPositionId = current.SubPositionId,
                 SubPositionName = current.SubPositionName,
                 Value = current.Value,
                 PreviousValueAdjusted = previous.Value,
                 DeltaPercentage = (previous.!= null || previous.Value != 0) ?
                                   (current.Value - previous.Value) / previous.Value : null,
             };

I assume the GroupBy() was added to enable the sum over the children. But apparently, the sum over a leave node does not simply give back the value of the property itself?

The DTO is defined as follows:

namespace API.Models
{
    public class myObjectDto
    {
        public int? PositionId { get; set; }
        public string PositionName { get; set; }
        public int SubPositionId { get; set; }
        public string SubPositionName { get; set; }
        public decimal? Value { get; set; }
        public decimal? PreviousValue { get; set; }
        public decimal? DeltaPercentage { get; set; }
    }
}

The parent entries have SubPositionId = null and SubPositionName = null, the child entries have those two fields filled. Leaf-nodes have children=null.

References

  • LINQ: How to join nested Lists in an ObjectCollection
  • LINQ inner join for nested list
  • LINQ JOIN Child Collection

来源:https://stackoverflow.com/questions/60079214/linq-nested-inner-join-does-not-join-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!