当子对象可以为 null 时,从子对象的属性返回值的更好方法

发布于 2024-12-07 12:07:37 字数 1929 浏览 0 评论 0原文

我尝试用一​​个例子来解释这一点:

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

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

现在我想将 Player 映射到 PlayerVM (ViewModel)

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

    public string TeamName {get; set;}
}

所以代码是这样的:

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

我想替换

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

为这样的内容:

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

使用通用 Lamba / Func 表达式可以吗?

<强><<编辑>>

感谢您的回答,我知道我可以使用 oneliners,但我实际上正在寻找一种访问嵌套子对象的通用方法。 (嵌套可能是 X 层深...)

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

如何做到这一点?

<<编辑2>> 一个可能的解决方案是使用此代码转换 Func 表达式 http://code.google.com /p/gim-projects/source/browse/presentations/CantDanceTheLambda/src/MemberNameParser.cs

为“Team.Country.Name”之类的字符串。

然后使用反射来访问属性。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-12-14 12:07:37

没有

vm.TeamName = p.Team.Name != null ? p.Team.Name : "-- no team --";

泛型,没有 lambda,但是如果你想用 oneliner 替换 if/else 块,这就是要走的路。

所以要清理整个映射,它将是

list.Add( new PlayerVM{
            PlayerFirstName = player.FirstName,    
            TeamName = player.Team.Name != null ? player.Team.Name : "-- no team --"
         });  

What about

vm.TeamName = p.Team.Name != null ? p.Team.Name : "-- no team --";

No generics, no lambda, but if you want to replace the if/else block with a oneliner, this is the way to go.

So to clean up the entire mapping it will be

list.Add( new PlayerVM{
            PlayerFirstName = player.FirstName,    
            TeamName = player.Team.Name != null ? player.Team.Name : "-- no team --"
         });  
屋檐 2024-12-14 12:07:37

我将在 Player 类中创建一个属性:

public string TeamName {
    get {
        return this.Team != null ? this.Team.Name : "-- no team --";
    }
}

I would create a property in the Player class:

public string TeamName {
    get {
        return this.Team != null ? this.Team.Name : "-- no team --";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文