本文介绍了查询调用批次和子批次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我使用下面的代码我正在尝试调用批次和子目录,但子批次为空。我知道我只返回批次查看,但我怎么称他们为



Hi was wondering can someone help me with this code below I am trying call the batches and subbatches but sub batches are null . I know I am only returning batches to view but how would I call them both

public ActionResult Index()
        {
            var batchesQuery = db.Batches;

            var Batches = batchesQuery.Select(batch => new BatchInfoView
            {
                BatchID = batch.BatchID,
                BatchName = batch.Name
            }).ToList();

            var SubBatches = batchesQuery.Select(subBatch => new SubBatchInfoView
            {
                SubBatchID = subBatch.SubBatches.Select(sbi => sbi.SubBatchID),
                SubBatchName = subBatch.SubBatches.Select(sbn => sbn.Code),
                SubBatchCount = subBatch.SubBatches.Select(sbc => sbc.SubBatchID).Count()
            }).ToList();

            return View(Batches);
        }





BatchInfoView





BatchInfoView

namespace SIMCardAdmin.Web.Models.Batch
{
    public class BatchInfoView
    {
        /// <summary>
        /// Flag to determine if this model is new.
        /// </summary>
        public bool IsNew { get; set; }

        /// <summary>
        /// The ID of the Batch.
        /// </summary>
        public int BatchID { get; set; }

        /// <summary>
        /// The name of the Batch, for display purposes.
        /// </summary>
        public string BatchName { get; set; }

        public List<SubBatchInfoView> SubBatches { get; set; }
    }
}







SubBatchInfoView






SubBatchInfoView

public class SubBatchInfoView
    {
        /// <summary>
        /// The ID of the SubBatch, use this to jump to a list for this SubBatch.
        /// </summary>
        public IEnumerable<int> SubBatchID { get; set; }

        /// <summary>
        /// The name of the SubBatch, for display purposes.
        /// </summary>
        public IEnumerable<string> SubBatchName { get; set; }

        /// <summary>
        /// The count of the SubBatch, for display purposes.
        /// </summary>
        public int SubBatchCount { get; set; }
    }

推荐答案

public class BatchInfoView
{
    public bool IsNew { get; set; }
    public int BatchID { get; set; }
    public string BatchName { get; set; }
    public List<SubBatchInfoView> SubBatches { get; set; }

    public int SubBatchCount
    {
        get { return SubBatches == null ? 0 : SubBatches.Count; }
    }
}

public class SubBatchInfoView
{
    public int SubBatchID { get; set; }
    public string SubBatchName { get; set; }
}



然后你需要加载子批次作为查询的一部分来加载批次:


Then you'll need to load the sub-batches as part of the query to load the batches:

var batches = db.Batches
    .Include(batch => batch.SubBatches)
    .Select(batch => new BatchInfoView
    {
        BatchID = batch.BatchID,
        BatchName = batch.Name,
        SubBatches = batch.SubBatches.Select(sb => new SubBatchInfoView
        {
            SubBatchID = sb.SubBatchID,
            SubBatchName = sb.Code,
        }).ToList()
    })
    .ToList();


这篇关于查询调用批次和子批次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 06:06