当子对象可以为 null 时,从子对象的属性返回值的更好方法
我尝试用一个例子来解释这一点:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有
泛型,没有 lambda,但是如果你想用 oneliner 替换 if/else 块,这就是要走的路。
所以要清理整个映射,它将是
What about
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
我将在
Player
类中创建一个属性:I would create a property in the
Player
class:可以在这里找到可能的解决方案:
获取使用反射和 Linq.Expression 的嵌套属性值
A possible solution can be found here:
Get Nested Property value using reflection and Linq.Expression