Ethan Pelton
Ethan Pelton

Reputation: 1796

how can I make entities enumerable

I am attempting to join to lists, add them to a viewmodel, and iterate through those lists in the view.

IList<trace> traces = db.traces.Where(t => t.bureau_rep == "MGS").Take(20).ToList();

IList<business> businesses = new List<business>();

var joined = from t in traces
    join b in businesses
    on t.businessid equals b.businessid
    select new vwhome { traces = t, business = b };

return View(joined);

The view model looks like this...

public class vwhome
{
    public trace traces { get; set; }
    public business business { get; set; }
}

In the view, I would like to display the list of traces, each of which has one business that should be displayed.

My problem is that trace and business are not enumerable, so I can't do this...

public class vwhome
{
    public IList<trace> traces { get; set; }
    public business business { get; set; }
}

How should I resolve this?

------------------------------------

Edit - This is clearly not a good solution, but at least it works...

This is now how I'm filling the viewmodel...

IList<trace> traces = db.traces.Where(t => t.bureau_rep == "MGS").Take(20).ToList();

        IList<business> businesses = new List<business>();

        foreach (var item in traces)
        {
            businesses.Add(db.businesses.Find(item.businessid));
        }
        var viewModel = new vwhome();
        viewModel.traces = traces;
        viewModel.business = businesses;

And this is my view...

@foreach(var item in Model.traces)
    {
    <tr>
        <td>@Html.DisplayFor(model => item.bureau_rep)</td>
        @foreach(var b in Model.business)
        {
            if(b.businessid == item.businessid)
            {
                <td>@Html.DisplayFor(model => b.name)</td>
            }
        }
    </tr>
    }

Upvotes: 0

Views: 95

Answers (4)

Jesse Anderson
Jesse Anderson

Reputation: 64

It seems to be a problem with how the trace relates to the business. Try working with the relationships between your trace and business classes. It appears that the trace class should have a many-to-one relationship to business within your entity models. Not knowing what your models for these classes look like, I recommend that you could make sure you have the appropriate references within the classes. if they aren't there, add these:

Add this to your trace class:

public virtual business business { get; set; }

Add the following to your business class:

public business()
{
    this.Traces = new List<trace>();
}

public virtual ICollection<trace> Traces { get; set; }

And within your tracemap class add the relationship:

this.HasOptional(t => t.business)
   .WithMany(t => t.Traces)
   .HasForeignKey(t => t.businessid);

Once you have this setup, try setting up your view model like this:

public class vmhome 
{
    public trace trace { get; set; }
    public business business { get; set; }
}

and use the relationships in your LINQ query

var joined = 
from t in traces select new vwhome 
{ 
    trace = t, 
    business = t.business
};

Then in your View, you can run through your references:

@Html.DisplayFor(modelItem => item.trace.prop_1)
@Html.DisplayFor(modelItem => item.business.name)

You could probably omit the business class from the vm and just exploit the relationship in your View:

@Html.DisplayFor(modelItem => item.trace.business.name)

I hope that helps.

Upvotes: 1

D Stanley
D Stanley

Reputation: 152634

You view is using a collection of vwhome objects, each of which contains a trace and a vusiness, so your view will look something like:

<ul>
@foreach (var item in Model)
{
    <li>@(item.trace) @(item.business)</li>
}
</ul>

Upvotes: 0

Sachin Kainth
Sachin Kainth

Reputation: 46760

If I understand you correctly you will need this in your Trace class

public virtual vwhome vwhome { get; set; }

In your vwhome class you need

public virtual ICollection<Trace > Traces { get; set; }

Upvotes: 0

Corey Adler
Corey Adler

Reputation: 16149

Your traces and business fields need to be strings, not of the actual object, and populate them with the trace name and the business. Then you can pass that along to your view to be property displayed on the screen.

The LINQ query you have near the top will return those as an IEnumerable<vwhome>.

Upvotes: 0

Related Questions