tag:blogger.com,1999:blog-15136575.post6438716711664356087..comments2023-10-17T12:00:16.772+01:00Comments on Code rant: Getting the Property Name From a Property ExpressionMike Hadlowhttp://www.blogger.com/profile/16441901713967254504noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-15136575.post-11971252419289872252013-02-19T20:18:21.515+00:002013-02-19T20:18:21.515+00:00Thanks Mike!Thanks Mike!Rubens Mariuzzohttps://www.blogger.com/profile/10135980656836281405noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-14205926932006034852011-11-29T11:48:56.452+00:002011-11-29T11:48:56.452+00:00Hi Mike,
Thanks for a great post, this has just m...Hi Mike,<br /><br />Thanks for a great post, this has just made it in to my current project.<br /><br />A came across an edge case as my class has some nullable dates which cause a boxing operation when the expression is validated. So I changed the the expression signature from Expression> to Expression> which avoids the boxing operation.<br /><br />See here for more details http://stackoverflow.com/questions/3567857/why-are-some-object-properties-unaryexpression-and-others-memberexpressionKeith Bloomhttps://www.blogger.com/profile/02976390053858341358noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-20041475416985075622010-09-19T12:42:31.495+01:002010-09-19T12:42:31.495+01:00Hi Mike,
My motivation to use it in Entity Framewo...Hi Mike,<br />My motivation to use it in Entity Framework with Include method and i need to get string like "Childs.Name". When I looking for solution first idea was to implement something like you sample. But child collections is problem for me because I do not know which expression use. For this purpose I implemented following interface:<br />LoadOptions options = new LoadOptions();<br />options.AddCollection(parent => parent.Childs).Add( child => child.Name);<br />But i still want to use single method with single lambda. Maybe you has any advices?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-36177372132111462772010-09-18T22:10:44.281+01:002010-09-18T22:10:44.281+01:00Hi Anonymous,
I guess it really depends what you ...Hi Anonymous,<br /><br />I guess it really depends what you want to use the string for. What does the representation of your collection need to look like. For example, my motivation for doing this work initially was so that I could provide a string to MVC's default binder. If I wanted to represent a collection, I would have do it like this: Parent.Child[0].ChildProperty, but your requirement might be different.Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-2969915628645689182010-09-18T09:49:19.627+01:002010-09-18T09:49:19.627+01:00Cool. But what about when i need select property N...Cool. But what about when i need select property Name from collection of childs.<br />class Parent{<br /> public IEnumerable Childs;<br />}<br /><br />class Child{<br /> public string Name;<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-17051983994145269122010-09-15T10:55:23.802+01:002010-09-15T10:55:23.802+01:00Nice, I didn't even know of String.Join, thank...Nice, I didn't even know of String.Join, thanks!Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-89497382504248939852010-09-15T10:24:23.019+01:002010-09-15T10:24:23.019+01:00Nice article, I've got similar code to remove ...Nice article, I've got similar code to remove 'magic' strings and make code refactor friendly.<br /><br />I do make use of .NET 4.0's new String.Join IEnumerable overload, not because of potential string concat performance but because it feels more in the spirit of .NET:<br /><br />return String.Join(".", GetProperties(propertyExpression).Select(x=>x.Name))Anonymousnoreply@blogger.com