Skip to content

Instantly share code, notes, and snippets.

@vendettamit
vendettamit / LinqVisualizerForIGrouping.cs
Created February 12, 2016 18:57
A snippet to print nested IGrouping items from a GroupBy Linq.
public static class LinqVisualizer
{
public static void Dump<Tkey, Telement>(this IEnumerable<IGrouping<Tkey, Telement>> source)
{
foreach (var item in source)
{
Console.WriteLine("Root Level Key : {0}", item.Key);
foreach (var group in item)
{
var context = new ConferenceDb();
var query = context.Set<Conference>().AsNoTracking();
var predicate = ExpressionBuilder.MakePredicate<Conference>(new List<ExpressionModel>(new[] {
new ExpressionModel() {
Operator = ExpressionBuilder.ExpressionOperators.Equals,
PropertyName = "Slug",
Value = "Test"
},
new ExpressionModel() {
@vendettamit
vendettamit / DynamicExpression.cs
Created February 11, 2016 16:21
Build a expression dynamically with the Type that only be known at runtimes.
public static class QueryableExtension
{
public static dynamic Build<Tobject>(this Tobject source, string propertyName)
{
var propInfo = typeof(Tobject).GetProperty(propertyName);
var parameter = Expression.Parameter(typeof(Tobject), "x");
var property = Expression.Property(parameter, propInfo);
@vendettamit
vendettamit / QueryableExtensionForSelectList.cs
Created February 5, 2016 20:36
A sample class to generate SelectList from a Queryable object.
public static class QueryableExtension
{
/// <summary>
/// <remarks>
/// Sampe Usage -
/// 1. queryableObject.GetSelectList(x => x.IdField, x => x.NameField);
/// 2. queryableObject.GetSelectList(x => x.IdField, x => x.NameField).WithDefaultItem("Select a project");
/// </remarks>
/// </summary>
/// <typeparam name="T"></typeparam>
MyDbContext db = new MyDbContext();
List<SelectListItem> selectedItems = new List<SelectListItem>();
if (type == null) return selectedItems;
if (type == typeof(TestDemo))
selectedItems = db.TestDemo.Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }).ToList();
if (type == typeof(TestDemo1))
selectedItems = db.TestDemo1.Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }).ToList();
private static Expression<Func<T,T,T>> BuildIt<T>()
{
var paramExprA = Expression.Parameter(typeof(T), "a");
var paramExprB = Expression.Parameter(typeof(T), "b");
var body = BinaryExpression.Add(paramExprA, paramExprB);
var lambda = Expression.Lambda<Func<T, T, T>>(body, paramExprA, paramExprB);
return lambda;
@vendettamit
vendettamit / DynamicProjectionUsingExpression.cs
Created February 1, 2016 20:21
This is a sample method to create dynamic LINQ expressions for Select().
public static class QueryableExtension
{
public static IEnumerable<SelectListItem> GetTable<T>(this IQueryable<T> source)
{
KeyValuePair<PropertyInfo, PropertyInfo> sourceDestPropMap1
= new KeyValuePair<PropertyInfo, PropertyInfo>(
// Text prop of selected item
typeof(SelectListItem).GetProperty("Text"),
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE80;
using EnvDTE90a;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using EnvDTE;
namespace Common
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using SampleService.Contracts;
using WcfDynamicProxy.Tests.Helper;
using NUnit.Framework;