Created
May 16, 2019 07:28
-
-
Save baralong/0538986acc5e25507e691d20c012f17a to your computer and use it in GitHub Desktop.
For a DAL POCO with int IDs link a child to a parent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Extensions | |
{ | |
public static void Link<TParent, TChild>(this TParent parent, TChild child, Expression<Func<TChild, int>> link) | |
where TParent : HasId | |
{ | |
if (parent == null || child == null) return; | |
if (!(link.Body is MemberExpression member)) return; | |
var propertyInfo = (PropertyInfo)member.Member; | |
propertyInfo.SetValue(child, parent.Id); | |
} | |
} | |
// usage | |
parent.Link(parent.Child, a => a.ParentId); | |
// this sets the value of parent.Child.ParentId to parent.Id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment