Click here to Skip to main content
16,016,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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

XML
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

XML
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; }
    }
Posted
Updated 2-Nov-15 4:04am
v3
Comments
John C Rayan 2-Nov-15 9:10am    
What is the relationship between Batch and SubBatch. When you load db.Batches make sure that you .include(SubBatch) too.
Member 11838038 2-Nov-15 9:11am    
Batch has many sub-batches
Member 11838038 2-Nov-15 9:18am    
I have tried .include(SubBatch) didnt work still throwing error
Richard Deeming 2-Nov-15 10:02am    
Update your question with the definition of both BatchInfoView and SubBatchInfoView.

At the moment, you're loading the SubBatchInfoView list, but not adding them to the corresponding BatchInfoViewList items.
Member 11838038 2-Nov-15 10:05am    
I have updated the question

1 solution

Your view models really don't make any sense. Each batch has a list of sub-batches, but then each sub-batch has a list of IDs, a list of names, and a count of the number of sub-batches.

I suspect you want something that looks like this:
C#
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:
C#
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();
 
Share this answer
 
Comments
Member 11838038 2-Nov-15 10:24am    
.Include(batch => batch.SubBatches) this part here is throwing cannot convert lambda expression to type string because is not delegate type
Richard Deeming 2-Nov-15 10:28am    
Do you have a "using System.Data.Entity;" statement at the top of your file? The Include extension method is defined in the System.Data.Entity.QueryableExtensions class.
Member 11838038 2-Nov-15 14:18pm    
thank you i really appreciate your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900