0

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

2
  • Do you want to display all names as a single string of names? Commented May 30, 2016 at 15:51
  • apologies for not being clear about that. I want to show one name for each entry. Commented May 30, 2016 at 15:55

3 Answers 3

2

Sorry for late reply, but this work for me:

@foreach (var item in Model.participants)
{
    <p>@item.name.first</p>
}
Sign up to request clarification or add additional context in comments.

Comments

1

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>
}

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.