Skip to content

Instantly share code, notes, and snippets.

View Ysovuka's full-sized avatar

Joshua Thompson Ysovuka

View GitHub Profile
@Ysovuka
Ysovuka / QueryableExtensions.cs
Last active May 6, 2018 22:28
Dynamically create selector for specified columns.
public static class QueryableExtensions
{
public static IQueryable<TResult> Select<TResult>(this IQueryable source, string[] columns)
{
var sourceType = source.ElementType;
var resultType = typeof(TResult);
var parameter = Expression.Parameter(sourceType, "e");
var bindings = columns.Select(column => Expression.Bind(
resultType.GetProperty(column), Expression.PropertyOrField(parameter, column)));
var body = Expression.MemberInit(Expression.New(resultType), bindings);
namespace Linq.Example.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var itemList = new List<Item>()
{
@Ysovuka
Ysovuka / dynamic-select.cs
Created May 14, 2018 03:16
Ability to return a dynamic amount of columns from a query.
private static IEnumerable<KeyValuePair<ConstantExpression, Expression>> GetPropertyExpressionsWithAllProperties(this ParameterExpression parameterExpression)
{
IEnumerable<string> propertiesToProvide = parameterExpression.Type.GetProperties().Select(r => r.Name);
return parameterExpression.GetPropertyExpressionsWithOnlyProvidedProperties(propertiesToProvide);
}
private static IEnumerable<KeyValuePair<ConstantExpression, Expression>> GetPropertyExpressionsWithOnlyProvidedProperties(this ParameterExpression parameterExpression, IEnumerable<string> providedProperties)
{
// Iterate through all properties for the source parameter expression type.
@Ysovuka
Ysovuka / dynamic-expression.cs
Last active May 14, 2018 03:34
Dynamic predicate expression.
public static object GetDefault(this Type targetType)
=> targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
public static MemberExpression GetPropertyExpression(this ParameterExpression parameterExpression, string propertyName)
{
// Get source type from parameter expression.
Type sourceType = parameterExpression.Type;
Debug.WriteLine($"Source Type: {sourceType.ToString()}");
// Get property information from source type.
@Ysovuka
Ysovuka / AsyncSocket.cs
Created July 7, 2018 20:14
Asynchronous Socket implementation.
using System;
namespace Wieldy.Net.Sockets
{
/// <summary>
/// Socket using 'SocketAsyncEventArgs' to communicate from client to server.
/// </summary>
public sealed partial class Socket
{
private readonly System.Net.Sockets.ProtocolType _protocol;
@Ysovuka
Ysovuka / IDisposable.h
Last active July 7, 2018 20:23
State Machine implementation in c++
/*
IDisposable.h
@purpose
An interface which allows for objects to be disposed.
Working with the RAII idiom, the dispose method should be called in the deconstructor of objects which inherit from it.
@author
Joshua Thompson
@Ysovuka
Ysovuka / Singleton.h
Created July 7, 2018 20:23
Singleton implementation in c++
/*
Singleton.h
@purpose
Singleton template class used to create a singleton object.
@author
Joshua Thompson
@modified
@Ysovuka
Ysovuka / RotatingCards.css
Created July 7, 2018 20:25
Rotating Cards in HTML
body {
margin-top: 60px;
font-size: 14px;
font-family: "Helvetica Nueue",Arial,Verdana,sans-serif;
background-color: #E5E9ED;
}
.btn:hover,
.btn:focus,
.btn:active{
outline: 0 !important;
@Ysovuka
Ysovuka / FloatingActionButton.css
Created July 7, 2018 20:26
Floating Action Button implementation.
body {
background-color: #F2F2F2;
margin: 0;
max-height: 100vh;
}
.header {
background: #4285f4;
width: 100%;
height: 56px;
body {
background: #020304;
}
body h1 {
position: fixed;
top: 40px;
left: 40px;
z-index: 6;
font-size: 20px;
font-weight: 900;