简体   繁体   中英

Adding a 3rd class in which 2 classes are subitems for linq query to end up displaying to json

This question is based on

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

in which this answer DID work fine

However, I'm adding a 3rd class that i want to group into "items"

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

New updated dotnetfiddle https://dotnetfiddle.net/IIBFKG

How do I ADD in CustomReportDefinition class to get displayed in the items or ReportData?

Actually you can add as many similar classes as you wish using exactly the same pattern:

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

UPDATE: Base on your comments, looks like you want to consolidate items and customItems . See if the following works for you:

var query = from d in reportData
            join r in reportDefinition on d.ReportGroupId equals r.ReportGroupId into items
            join cr in customReportDefinition on d.ReportGroupId equals cr.ReportGroupId into customItems
            select new
            {
                d.ReportGroupName,
                items = items.Select(r => new
                {
                    r.Id, r.ReportGroupNameDef, r.SortOrder, r.ReportGroupId, r.Type
                }).Concat(customItems.Select(cr => new
                {
                    cr.Id, cr.ReportGroupNameDef, cr.SortOrder, cr.ReportGroupId, cr.Type
                })).ToList(),
                d.ReportGroupId
            };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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