Click here to Skip to main content
15,888,239 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: how to create RDLC report using multiple sql columns with same values to count in vb.net Pin
DHARM PAL6-Feb-15 2:48
DHARM PAL6-Feb-15 2:48 
QuestionHow To Start a Remote Desktop Connection to remote host from VB Pin
Member 111939643-Feb-15 4:57
Member 111939643-Feb-15 4:57 
AnswerRe: How To Start a Remote Desktop Connection to remote host from VB Pin
Eddy Vluggen3-Feb-15 7:23
professionalEddy Vluggen3-Feb-15 7:23 
QuestionA C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 8:12
dilkonika2-Feb-15 8:12 
AnswerRe: A C# DLL produce error on VB.net project Pin
Dave Kreskowiak2-Feb-15 8:24
mveDave Kreskowiak2-Feb-15 8:24 
GeneralRe: A C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 8:42
dilkonika2-Feb-15 8:42 
GeneralRe: A C# DLL produce error on VB.net project Pin
Richard Deeming2-Feb-15 8:58
mveRichard Deeming2-Feb-15 8:58 
GeneralRe: A C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 9:10
dilkonika2-Feb-15 9:10 
These are the codes in that dll related to my method :
Quote:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace EntityFramework.Utilities
{
public static class EFQueryHelpers
{
///
/// Loads a child collection in a more efficent way than the standard Include. Will run all involved queries as NoTracking
///

/// <typeparam name="T">
/// <typeparam name="TChild">
/// <param name="query" />
/// <param name="context" />
/// <param name="collectionSelector" />The navigation property. It can be filtered and sorted with the methods Where,OrderBy(Descending),ThenBy(Descending)
/// <returns>
public static EFUQueryable<t> IncludeEFU<t, tchild="">(this IQueryable<t> query, DbContext context, Expression<func<t, ienumerable<tchild="">>> collectionSelector)
where T : class
where TChild : class
{
var octx = (context as IObjectContextAdapter).ObjectContext;
var cSpaceTables = octx.MetadataWorkspace.GetItems<entitytype>(DataSpace.CSpace);
var cSpaceType = cSpaceTables.Single(t => t.Name == typeof(T).Name); //Use single to avoid any problems with multiple tables using the same type
var keys = cSpaceType.KeyProperties;
if (keys.Count > 1)
{
throw new InvalidOperationException("The include method only works on single key entities");
}

var fkGetter = GetForeignKeyGetter<t, tchild="">(cSpaceTables);

PropertyInfo pkInfo = typeof(T).GetProperty(keys.First().Name);
var pkGetter = MakeGetterDelegate<t>(pkInfo);

var childCollectionModifiers = new List<methodcallexpression>();
var childProp = SetCollectionModifiersAndGetChildProperty<t, tchild="">(collectionSelector, childCollectionModifiers);
var setter = MakeSetterDelegate<t>(childProp);

var e = new IncludeExecuter<t>
{
ElementType = typeof(TChild),
SingleItemLoader = (parent) =>
{
if (parent == null)
{
return;
}
var children = octx.CreateObjectSet<tchild>();
var lambdaExpression = GetRootEntityToChildCollectionSelector<t, tchild="">(cSpaceType);

var q = ApplyChildCollectionModifiers<tchild>(children, childCollectionModifiers);

var rootPK = pkGetter((T)parent);
var param = Expression.Parameter(typeof(TChild), "x");
var fk = GetFKProperty<t, tchild="">(cSpaceTables);
var body = Expression.Equal(Expression.Property(param, fk), Expression.Constant(rootPK));
var where = Expression.Lambda<func<tchild, bool="">>(body, param);

q = q.AsNoTracking().Where(where);

setter((T)parent, q.ToList());
},
Loader = (rootFilters, parents) =>
{
var baseType = typeof(T).BaseType != typeof(object) ? typeof(T).BaseType : typeof(T);

dynamic dynamicSet = octx.GetType()
.GetMethod("CreateObjectSet", new Type[] { })
.MakeGenericMethod(baseType)
.Invoke(octx, new Object[] { });

var set = dynamicSet.OfType<t>() as ObjectQuery<t>;

IQueryable<t> q = set;
foreach (var item in rootFilters)
{
var newSource = Expression.Constant(q);
var arguments = Enumerable.Repeat(newSource, 1).Concat(item.Arguments.Skip(1)).ToArray();
var newMethods = Expression.Call(item.Method, arguments);
q = q.Provider.CreateQuery<t>(newMethods);
}

var lambdaExpression = GetRootEntityToChildCollectionSelector<t, tchild="">(cSpaceType);

var childQ = q.SelectMany(lambdaExpression);
childQ = ApplyChildCollectionModifiers<tchild>(childQ, childCollectionModifiers);

var dict = childQ.AsNoTracking().ToLookup(fkGetter);
var list = parents.Cast<t>().ToList();

foreach (var parent in list)
{
var prop = pkGetter(parent);
var childs = dict.Contains(prop) ? dict[prop].ToList() : new List<tchild>();
setter(parent, childs);
}
}
};

return new EFUQueryable<t>(query.AsNoTracking()).Include(e);
}

private static IQueryable<tchild> ApplyChildCollectionModifiers<tchild>(IQueryable<tchild> childQ, List<methodcallexpression> childCollectionModifiers) where TChild : class
{
foreach (var item in childCollectionModifiers)
{
switch (item.Method.Name)
{
case "Where":
childQ = childQ.Where((Expression<func<tchild, bool="">>)item.Arguments[1]);
break;
case "OrderBy":
case "ThenBy":
case "OrderByDescending":
case "ThenByDescending":
childQ = SortQuery(childQ, item, item.Method.Name);
break;
default:
throw new NotSupportedException("The method " + item.Method.Name + " is not supported in the child query");
}
}
return childQ;
}

private static PropertyInfo SetCollectionModifiersAndGetChildProperty<t, tchild="">(Expression<func<t, ienumerable<tchild="">>> collectionSelector, List<methodcallexpression> childCollectionModifiers)
where T : class
where TChild : class
{
var temp = collectionSelector.Body;
while (temp is MethodCallExpression)
{
var mce = temp as MethodCallExpression;
childCollectionModifiers.Add(mce);
temp = mce.Arguments[0];
}
childCollectionModifiers.Reverse(); //We parse from right to left so reverse it
if (!(temp is MemberExpression))
{
throw new ArgumentException("Could not find a MemberExpression", "collectionSelector");
}

var childProp = (temp as MemberExpression).Member as PropertyInfo;
return childProp;
}

private static Func<tchild, object=""> GetForeignKeyGetter<t, tchild="">(System.Collections.ObjectModel.ReadOnlyCollection<entitytype> cSpaceTables)
where T : class
where TChild : class
{
var fkInfo = GetFKProperty<t, tchild="">(cSpaceTables);
var fkGetter = MakeGetterDelegate<tchild>(fkInfo);
return fkGetter;
}

private static PropertyInfo GetFKProperty<t, tchild="">(System.Collections.ObjectModel.ReadOnlyCollection<entitytype> cSpaceTables)
where T : class
where TChild : class
{
var cSpaceChildType = cSpaceTables.Single(t => t.Name == typeof(TChild).Name); //Use single to avoid any problems with multiple tables using the same type
var fk = cSpaceChildType.NavigationProperties.First(n => n.ToEndMember.GetEntityType().Name == typeof(T).Name).GetDependentProperties().First();
var fkInfo = typeof(TChild).GetProperty(fk.Name);
return fkInfo;
}

private static IQueryable<tchild> SortQuery<tchild>(IQueryable<tchild> query, MethodCallExpression item, string method)
{
var body = (item.Arguments[1] as LambdaExpression);

MethodCallExpression call = Expression.Call(
typeof(Queryable),
method,
new[] { typeof(TChild), body.Body.Type },
query.Expression,
Expression.Quote(body));

return (IOrderedQueryable<tchild>)query.Provider.CreateQuery<tchild>(call);
}

private static Expression<func<t, ienumerable<tchild="">>> GetRootEntityToChildCollectionSelector<t, tchild="">(EntityType cSpaceType)
where T : class
where TChild : class
{
var parameter = Expression.Parameter(typeof(T), "t");
var memberExpression = Expression.Property(parameter, cSpaceType.NavigationProperties.First(p => p.ToEndMember.GetEntityType().Name == typeof(TChild).Name).Name);
var lambdaExpression = Expression.Lambda<func<t, ienumerable<tchild="">>>(memberExpression, parameter);
return lambdaExpression;
}

static Action<t, object=""> MakeSetterDelegate<t>(PropertyInfo property)
{
MethodInfo setMethod = property.GetSetMethod();
if (setMethod != null && setMethod.GetParameters().Length == 1)
{
var target = Expression.Parameter(typeof(T));
var value = Expression.Parameter(typeof(object));
var body = Expression.Call(target, setMethod,
Expression.Convert(value, property.PropertyType));
return Expression.Lambda<action<t, object="">>(body, target, value)
.Compile();
}
else
{
return null;
}
}

static Func<x, object=""> MakeGetterDelegate<x>(PropertyInfo property)
{
MethodInfo getMethod = property.GetGetMethod();
if (getMethod != null)
{
var target = Expression.Parameter(typeof(X));
var body = Expression.Call(target, getMethod);
Expression conversion = Expression.Convert(body, typeof(object));
return Expression.Lambda<func<x, object="">>(conversion, target)
.Compile();
}
else
{
return null;
}
}
}
}

GeneralRe: A C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 9:14
dilkonika2-Feb-15 9:14 
GeneralRe: A C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 9:15
dilkonika2-Feb-15 9:15 
GeneralRe: A C# DLL produce error on VB.net project Pin
Richard Deeming2-Feb-15 9:29
mveRichard Deeming2-Feb-15 9:29 
GeneralRe: A C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 9:56
dilkonika2-Feb-15 9:56 
GeneralRe: A C# DLL produce error on VB.net project Pin
Richard Deeming2-Feb-15 10:05
mveRichard Deeming2-Feb-15 10:05 
GeneralRe: A C# DLL produce error on VB.net project Pin
dilkonika2-Feb-15 9:59
dilkonika2-Feb-15 9:59 
QuestionMicrosoft.Office.Interop.Excel.Application and pictures Pin
jkirkerx2-Feb-15 8:11
professionaljkirkerx2-Feb-15 8:11 
AnswerRe: Microsoft.Office.Interop.Excel.Application and pictures Pin
Kenneth Haugland3-Feb-15 8:14
mvaKenneth Haugland3-Feb-15 8:14 
General[Heading over to the Microsoft Forum for this] Pin
jkirkerx3-Feb-15 9:24
professionaljkirkerx3-Feb-15 9:24 
QuestionEntity Framework : Filter child entities Pin
dilkonika31-Jan-15 7:15
dilkonika31-Jan-15 7:15 
AnswerRe: Entity Framework : Filter child entities Pin
Kenneth Haugland31-Jan-15 21:35
mvaKenneth Haugland31-Jan-15 21:35 
GeneralRe: Entity Framework : Filter child entities Pin
dilkonika1-Feb-15 5:02
dilkonika1-Feb-15 5:02 
AnswerRe: Entity Framework : Filter child entities Pin
Kenneth Haugland1-Feb-15 19:09
mvaKenneth Haugland1-Feb-15 19:09 
GeneralRe: Entity Framework : Filter child entities Pin
dilkonika2-Feb-15 4:45
dilkonika2-Feb-15 4:45 
GeneralRe: Entity Framework : Filter child entities Pin
Kenneth Haugland2-Feb-15 4:55
mvaKenneth Haugland2-Feb-15 4:55 
GeneralRe: Entity Framework : Filter child entities Pin
dilkonika2-Feb-15 5:13
dilkonika2-Feb-15 5:13 
QuestionOverriding events in an inherited Control Pin
Sam Marrocco30-Jan-15 9:20
Sam Marrocco30-Jan-15 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.