c#

Shorten Your Linq Expressions

There is an interesting style of Linq expression that I am seeing a lot in code, including open source projects. It isn’t a big deal, but it damages readability. It looks like this: var result = someEnumerable.Where(e => e.IsCondition).FirstOrDefault(); var result = someEnumerable.Where(e => e.IsCondition).SingleOrDefault(); var result = someEnumerable.Where(e => e.IsCondition).Count(); Can you spot it? […]

Get The Topmost Assembly Name

This is just a useful snippet that will get you the top most assembly name. Especially useful if you have code in a NuGet package that will be consumed by some other library… and eventually by an application. public string GetTopLevelAssembly() { StackFrame[] stackTraceFrames = new StackTrace().GetFrames(); return stackTraceFrames .Select(f => f.GetMethod().ReflectedType.AssemblyQualifiedName) .Distinct() .Last(); }