简体   繁体   English

Linq查询中未加入的数据不会输出到json中,只有出现在2个类中的相关数据才显示

[英]Data in Linq query not in join is not in output to json only those that are related in 2 classes are showing up

The basis of this question is from this question: 这个问题的基础来自于这个问题:

Combine 2 classes with adding data and 1 table has a colllection list of the other table and wanting to use linq to display 将2个类与添加数据组合在一起,其中1个表具有另一个表的集合列表,并希望使用linq进行显示

In which I "thought" the problem was solved. 我在其中“解决”了问题。

However as I added in a new object to the List, now this join query does not output it 但是,正如我在列表中添加新对象一样,现在此联接查询不会将其输出

reportData.Add(new ReportData() {ReportGroupId = 3, ReportGroupName = "Straggler", SortOrder = 3, Type = 1});


var reports = reportDefinition.GroupBy(r=>r.ReportGroupId);
    var query = reportData.Join(reports, d => d.ReportGroupId, gr => gr.Key, 
                                (r,gr) => new
                                {
                                  r.ReportGroupName,
                                  items = gr.ToList(),
                                  r.ReportGroupId
                                });

Here is the dotNetFiddle https://dotnetfiddle.net/IIBFKG 这是dotNetFiddle https://dotnetfiddle.net/IIBFKG

Why doesn't the item that I added to the ReportData not show up? 为什么我添加到ReportData的项目没有显示? Is it the type of JOIN in Linq? 这是Linq中的JOIN类型吗?

I think the linked question was not answered correctly. 我认为链接的问题未正确回答。

Looks like all you need is a simple Group Join : 看起来您只需要一个简单的Group Join

var query = 
    from d in reportData
    join r in reportDefinition on d.ReportGroupId equals r.ReportGroupId into items
    select new
    {
        d.ReportGroupName,
        items = items.ToList(),
        d.ReportGroupId
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM