Cleanest Way To Map Entity To DTO With Linq Select?

Just use AutoMapper. Example: Mapper.CreateMap<Address, AddressDTO>(); Mapper.CreateMap<Person, PersonDTO>(); Your query will execute when the mapping is performed but if there are fields in the entity that you’re not interested use Project().To<> which is available both for NHibernate and EntityFramework. It will effectively do a select on the fields specified in the mapping configurations.

Given a Member Access lambda expression, convert it to a specific string representation with full access path

public string GetPath<T>(Expression<Func<T, object>> expr) { var stack = new Stack<string>(); MemberExpression me; switch (expr.Body.NodeType) { case ExpressionType.Convert: case ExpressionType.ConvertChecked: var ue = expr.Body as UnaryExpression; me = ((ue != null) ? ue.Operand : null) as MemberExpression; break; default: me = expr.Body as MemberExpression; break; } while (me != null) { stack.Push(me.Member.Name); me = me.Expression … Read more

Lambda property value selector as parameter

private string MyMethod(int testParameter, Func<MyObject, string> selector) { return selector(_myObject); } When using Func delegates, the last parameter is the return type and the first N-1 are the argument types. In this case, there is a single MyObject argument to selector and it returns a string. You can invoke it like: string name = _myClassInstance.MyMethod(1, … Read more

Resolving extension methods/LINQ ambiguity

This is probably one of those rare cases where it makes sense to use an extern alias. In the properties page for the reference to System.Core (i.e. under References, select System.Core, right-click and select “Properties”), change the “Aliases” value to “global,SystemCore” (or just “SystemCore” if it’s blank to start with). Then in your code, write: … Read more

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş