【问题标题】:Data in Linq query not in join is not in output to json only those that are related in 2 classes are showing upLinq 查询中未加入的数据未输出到 json,仅显示 2 个类中相关的数据
【发布时间】:2016-03-24 18:13:48
【问题描述】:

这个问题的基础来自这个问题:

Combine 2 classes with adding data and 1 table has a colllection list of the other table and wanting to use linq to display

我“认为”问题解决了。

但是,当我在列表中添加了一个新对象时,现在这个连接查询不会输出它

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
                                });

这里是 dotNetFiddle https://dotnetfiddle.net/IIBFKG

为什么我添加到 ReportData 中的项目没有显示?是Linq中JOIN的类型吗?

【问题讨论】:

  • 因为你在表演inner join,而ReportReportGroupId = 3没有@

标签: c# json linq linq-to-sql linq-to-entities


【解决方案1】:

我认为链接的问题没有正确回答。

看起来你只需要一个简单的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
    };

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
相关资源
最近更新 更多