简体   繁体   中英

Add a list to a list Linq C#

I have a POCO like this:

public class BatchTypesByNetwork
{
    public Guid BatchTypeId { get; set; }
    public string Name { get; set; }
    public bool CanSchedule { get; set; }
    public string Description { get; set; }
    public Guid? NetworkTypeId { get; set; }
    public string ReportDisplayId { get; set; }

    public IEnumerable<BatchScheduleParameters> ScheduleParameters { get; set; }
}

and

public class BatchScheduleParameters
{
    public Guid BatchTypeId { get; set; }
    public Guid ParameterId { get; set; }
    public string ParameterName { get; set; }
    public string ParameterType { get; set; }
    public string ParameterValue { get; set; }
    public bool IsMultiSelection { get; set; }
}

I am doing like this:

First I get all the BatchTypesByNetwork like this:

    var allBatchTypes = await query.Join(_networkTypesRepository, bt => bt.NetworkTypeId, nt => nt.Id, (bt, nt) => new {bt, nt})
                                        .Where(m => m.nt.Name == convertToString(networkType))
                                        .Select(m => 
                                                new BATCH.BatchTypesByNetwork
                                                {
                                                   BatchTypeId = m.bt.Id,
                                                   Name = m.bt.Name,
                                                   CanSchedule = m.bt.CanSchedule,
                                                   Description = m.bt.Description,
                                                   NetworkTypeId = m.bt.NetworkTypeId,
                                                   ReportDisplayId = m.bt.ReportDisplayId
                                               }).AsNoTracking().ToListAsync(serviceContext.CancellationToken);

Then I loop through batchTypes like this and assign BatchScheduleParameters list to individual batchType

        foreach (var batchType in allBatchTypes)
        {
            var allScheduleParameters = await (from bp in _batchParametersRepository
                                               join bt in _batchParameterTypesRepository
                                               on bp.BatchParameterTypeId equals bt.Id
                                               where bp.IsRequired == true
                                               && bp.BatchTypeId == batchType.BatchTypeId

                                               select new BATCH.BatchScheduleParameters
                                               {
                                                   BatchTypeId = bt.Id,
                                                   ParameterId = bp.BatchParameterTypeId,
                                                   ParameterName = bp.Name,
                                                   ParameterType = bt.Name,
                                                   IsMultiSelection = bp.AllowMultipleSelection == true ? true : false,
                                               }
                                       ).AsNoTracking().ToListAsync(serviceContext.CancellationToken);

            batchType.ScheduleParameters = allScheduleParameters;
        }

Is it possible to do it in one query without for loop.

As @Fabio suggested in his comment, set up a one-to-many relation between BatchTypesByNetwork and BatchScheduleParameters (using Fluent API, Data Annotations or EF conventions) so you can take advantage of the

.Include(i => i.ScheduleParameters)

when querying the _networkTypesRepository.

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