Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6652773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:10:58+00:00 2026-05-26T01:10:58+00:00

I try to explain this with an example: public class Player { public string

  • 0

I try to explain this with an example:

public class Player
{
    public string FirstName {get; set;}
    public Team Team {get; set;}
}

public class Team
{
    public string Name {get; set;}
}

Now I want to map a Player to a PlayerVM (ViewModel)

public class PlayerVM
{
    public string PlayerFirstName {get; set;}

    public string TeamName {get; set;}
}

So the code is something like:

public List<PlayerVM> GetPlayers()
{
    // Lazy loading enabled,
    // so the Team child objects (if present, will be retrieved)
    var players = Database.GetPlayers();

    var list = new List<PlayerVM>();
    foreach (var player in players)
    {
        var vm = new PlayerVM();

        vm.PlayerFirstName = player.FirstName;

        if (player.Team != null)
        {
            vm.TeamName = player.Team.Name;
        }
        else
        {
            vm.TeamName = "-- no team --";
        }

        list.Add(vm);
    }

    return list;
}

I want to replace

if (player.Team != null)
{
    vm.TeamName = player.Team.Name;
}
else
{
    vm.TeamName = "-- no team --";
}

by something like:

vm.TeamName = Utils.GetProperty<Player>(p => p.Team.Name, "-- no team --");

is this possible using generic Lamba / Func expressions ?

<< Edit >>

Thanks for the answers, I know I can use oneliners, but I was actually looking for a generic way to access nested child objects. (The nesting could be X levels deep…)

string countryName = Utils.GetProperty<Player>(p => p.Team.Country.Name, "-- no country --");

How to do this ?

<< Edit 2 >>
A possible solution would to convert the Func Expression using this code
http://code.google.com/p/gim-projects/source/browse/presentations/CantDanceTheLambda/src/MemberNameParser.cs

to a string like “Team.Country.Name”.

Then use reflection to access the properties.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:10:58+00:00Added an answer on May 26, 2026 at 1:10 am

    A possible solution can be found here:

    Get Nested Property value using reflection and Linq.Expression

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll try to explain this with an example. I'm writing a chat application. There
The title isn't very clear but I'll try to explain. Having this class: class
I'll try my best to explain how I'm trying to set up this system.
This might be a little hard to explain, but I will try. I want
Is it possible to do something like this? class A { public virtual string
I'll try to explain this better on another question. This is the query that
I'll try to explain this problem the best way i can with code: double
I' still newby to this so I'll try to explain what I'm doing. Basically
I've been struggling for a while with this one; I'll try to explain it
Okay, this scenario is a little complicated, but I will try to explain it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.