xyz
xyz

Reputation: 61

Model binding for multi list in linq

I'm working on an application to track opc that are coming through a web application. The application won't sell products, and it's designed to track opc as they come in via phone.

I'm still new to ASP.NET MVC, so I'm still learning the ropes. I'm trying to use model binding to get this to work.

public class TemplateViewModel
{
    public string templateName { get; set; }

    public List<RequestViewModel> requests { get; set; }

}

public class CategoryViewModel
{
    public int categoryId { get; set; }

    public string categoryName { get; set; }
}

public class ItemViewModel
{
    public string itemName { get; set; }
    public DateTime dueDate { get; set; }

    public string notes { get; set; }
}

public class RequestViewModel
{
    public List<CategoryViewModel> category { get; set; }

    public List<ItemViewModel> items { get; set; }

}

I have a viewmodel like a above and i have return the data like below

        var query = _sqlDbContext.RequestTemplates
            .Join(_sqlDbContext.RequestTemplateItems,
            w => w.RequestTemplateID,
            m => m.RequestTemplateID,
            (w, m) => new { w.TemplateName, m.RequestCategoryID, m.ItemName, m.DueDate, m.Notes })
            .Join(_sqlDbContext.RequestCategories,
            k => k.RequestCategoryID,
            opt => opt.RequestCategoryID,
            (k, opt) => new { k.ItemName, k.Notes, k.TemplateName, opt.RequestCategoryName, opt.RequestCategoryID, k.DueDate });

now i want to return data like List<> of data .

up to now my work

            model.templateName = query.Select(c => c.TemplateName);
        model.requests = query.Select(c => new RequestViewModel
        {
            category = new CategoryViewModel { categoryId = c.RequestCategoryID, categoryName = c.RequestCategoryName }
         ,
            items = new List<ItemViewModel> { dueDate = c.DueDate, itemName = c.ItemName, notes = c.Notes }
        });

but this is wrong , any on help me on it ?

Upvotes: 0

Views: 337

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27376

If understand correctly requirements, your query should look like this one:


var query = _sqlDbContext.RequestTemplates
    .Select(rt => new TemplateViewModel
    {
        templateName = rt.TemplateName,
        requests = _sqlDbContext.RequestTemplateItems
            .Where(ti => ti.RequestTemplateID == rt.RequestTemplateID)
            .Select(ti => new RequestViewModel 
            {
                category = new CategoryViewModel 
                {
                    categoryId = ti.RequestCategoryID,
                    categoryName = ti.RequestCategory.RequestCategoryName 
                },
                
                items = new List<ItemViewModel> { new ItemViewModel 
                {
                    itemName = ti.ItemName,
                    dueDate = ti.DueDate,
                    notes = ti.Notes 
                }}
            })
            .ToList()
    });

Upvotes: 1

Related Questions