Alfred Shaker
Alfred Shaker

Reputation: 135

Using Linq to get value from a model asp.net mvc

I have a model that is not very simple and straightforward and I want to get certain data from it but I am not too familiar with Linq or what command to use. Here is the model code:

public class RootObject
{
    public string _id { get; set; }
    public string court { get; set; }
    public string type { get; set; }
    public string caption { get; set; }
    public Case cases { get; set; }
    public Dates dates { get; set; }
    public Judge judge { get; set; }
    public Balance balance { get; set; }
    public Sentence sentence { get; set; }
    public List<Charge> charges { get; set; }
    public List<Participant> participants { get; set; }
}

public class Participant
{
    public string _id { get; set; }
    public string type { get; set; }
    public int partyNumber { get; set; }
    public Name2 name { get; set; }
    public Address address { get; set; }
    public Phone phone { get; set; }
    public Birth birth { get; set; }
    public List<Cost> costs { get; set; }
    public List<Receipt> receipts { get; set; }
}

public class Name2
{
    public string prefix { get; set; }
    public string first { get; set; }
    public string middle { get; set; }
    public string last { get; set; }
    public string suffix { get; set; }
    public string company { get; set; }
    public string full { get; set; }
}

I am basically trying to get to the first and last name or full name in that Name2 class through the root object and participants list. I am using asp.net 5 and mvc 6 and linq to call the objects, but cannot seem to get the value due to my linq being incorrect. The model is passed as in IEnumerable list to the view class, and here is the code used to try and get the name:

@foreach (var item in Model)
{
    @item.participants.Select(i => i.name.full);
}

Any help would be appreciated.

Thank you for your time

Upvotes: 0

Views: 1509

Answers (3)

Pieter P.
Pieter P.

Reputation: 109

Sorry for late reply, but this work for me:

@foreach (var item in Model.participants)
{
    <p>@item.name.first</p>
}

Upvotes: 2

Pedro Fernandes Filho
Pedro Fernandes Filho

Reputation: 539

Try this:

@foreach (var item in Model)
{
    @foreach (var item2 in item.participants)
    {
        <p>@item2.name.full</p>
        <p>@item2.name.first</p>
        <p>@item2.name.last</p>
    }
}

Other option - one name for each entry:

@foreach (var item in Model)
{
    <p>@item.participants.FirstOrDefault()?.name.full</p>
    <p>@item.participants.FirstOrDefault()?.name.first</p>
    <p>@item.participants.FirstOrDefault()?.name.last</p>
}

Upvotes: 1

Andrei
Andrei

Reputation: 56716

Not entirely clear how you want to show these names, but let's assume you want them in form of "name 1, name 2, name 3". Then this becomes:

@String.Join(", ", item.participants.Select(p => p.name.first + " " + p.name.last))

If you however want to show only one name per item, say the first one, then you can use First():

@item.participants.First(p => p.name.full)

Finally, if the participant list could be empty for some reason, use FirstOrDefault. This will output null for empty list, so make sure to handle it correctly!

Upvotes: 0

Related Questions