Click here to Skip to main content
15,886,664 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Is there a parser for .NET expression trees? Pin
honey the codewitch30-Jul-20 11:56
mvahoney the codewitch30-Jul-20 11:56 
GeneralRe: Is there a parser for .NET expression trees? Pin
Sander Rossel30-Jul-20 12:08
professionalSander Rossel30-Jul-20 12:08 
GeneralRe: Is there a parser for .NET expression trees? Pin
Member 1402550329-Jul-20 21:59
Member 1402550329-Jul-20 21:59 
GeneralRe: Is there a parser for .NET expression trees? Pin
Sander Rossel30-Jul-20 11:37
professionalSander Rossel30-Jul-20 11:37 
GeneralRe: Is there a parser for .NET expression trees? Pin
quantasm2329-Jul-20 22:52
quantasm2329-Jul-20 22:52 
AnswerRe: Is there a parser for .NET expression trees? Pin
ZevSpitz30-Jul-20 1:43
professionalZevSpitz30-Jul-20 1:43 
GeneralRe: Is there a parser for .NET expression trees? Pin
honey the codewitch30-Jul-20 2:05
mvahoney the codewitch30-Jul-20 2:05 
AnswerRe: Is there a parser for .NET expression trees? Pin
buckrogerz30-Jul-20 4:51
buckrogerz30-Jul-20 4:51 
So not sure if this is what your looking for but I used the System.Linq.Expressions namespace
to build a dynamic link query to be executed against EF.

here is an incomplete sample for syntax example:
propertyToUse = workingProperty.Substring(0, workingProperty.IndexOf('.'));
                Type propertyToUseType = GetEntityType(propertyToUse, incomingParentType);

                ParameterExpression propertyToUseParameterExpression = Expression.Parameter(propertyToUseType, propertyToUse.Substring(0,1));
                Expression parentExpression = Expression.Property(workingExpression, propertyToUse);

                if (parentExpression.Type.IsGenericType &&
                    typeof(IEnumerable<>)
                        .MakeGenericType(parentExpression.Type.GetGenericArguments())
                        .IsAssignableFrom(parentExpression.Type))
                {
                    Expression childExpression = BuildPropertyExpression(propertyToUseParameterExpression,
                        workingProperty, comparisonOperation, compareValue);
                    Type func = typeof(Func<,>);
                    Type genericFunc = func.MakeGenericType(propertyToUseType, typeof(bool));
                    LambdaExpression predicate =
                        Expression.Lambda(genericFunc, childExpression, propertyToUseParameterExpression);

                    //we have call the AsQueryable on the collection since we don't have the compiler working for us and we need to use the any method
                    MethodInfo asQueryableMethod = typeof(Queryable).GetMethods()
                        .Where(m => m.Name == "AsQueryable")
                        .Single(m => m.IsGenericMethod)
                        .MakeGenericMethod(propertyToUseType);
                    Expression asQueryableExpression = Expression.Call(null, asQueryableMethod, parentExpression);

                    //call the any method with the lambda expression we set up
                    MethodInfo anyMethod = typeof(Queryable).GetMethods()
                        .Where(m => m.Name == "Any")
                        .Single(m => m.GetParameters().Length == 2)
                        .MakeGenericMethod(propertyToUseType);
                    returnValue = Expression.Call(
                        null,
                        anyMethod,
                        asQueryableExpression, //the source
                        predicate); // the lambda expression
                }


There are other factories off the Expression class to help create parts this snip it does not show like

valueExpression = Expression.Constant(compareValue)


and

returnValue = Expression.Equal(workingExpression, valueExpression);


Hope this helps
Buckrogerz

AnswerRe: Is there a parser for .NET expression trees? Pin
nassimi30-Jul-20 21:33
nassimi30-Jul-20 21:33 
GeneralRe: Is there a parser for .NET expression trees? Pin
honey the codewitch31-Jul-20 0:58
mvahoney the codewitch31-Jul-20 0:58 
GeneralRe: Is there a parser for .NET expression trees? Pin
nassimi21-Jun-22 22:57
nassimi21-Jun-22 22:57 
GeneralDo you all use wysiwyg web site editors. Pin
Ron Anders29-Jul-20 3:40
Ron Anders29-Jul-20 3:40 
GeneralRe: Do you all use wysiwyg web site editors. PinPopular
DerekT-P29-Jul-20 3:47
professionalDerekT-P29-Jul-20 3:47 
GeneralRe: Do you all use wysiwyg web site editors. Pin
SortaCore29-Jul-20 20:06
SortaCore29-Jul-20 20:06 
GeneralRe: Do you all use wysiwyg web site editors. Pin
honey the codewitch29-Jul-20 3:54
mvahoney the codewitch29-Jul-20 3:54 
GeneralRe: Do you all use wysiwyg web site editors. Pin
Joan M29-Jul-20 4:01
professionalJoan M29-Jul-20 4:01 
GeneralRe: Do you all use wysiwyg web site editors. Pin
F-ES Sitecore29-Jul-20 5:11
professionalF-ES Sitecore29-Jul-20 5:11 
GeneralRe: Do you all use wysiwyg web site editors. Pin
Ron Anders29-Jul-20 5:44
Ron Anders29-Jul-20 5:44 
RantRe: Do you all use wysiwyg web site editors. Pin
Dan Neely29-Jul-20 6:42
Dan Neely29-Jul-20 6:42 
GeneralRe: Do you all use wysiwyg web site editors. Pin
kmoorevs29-Jul-20 8:59
kmoorevs29-Jul-20 8:59 
GeneralRe: Do you all use wysiwyg web site editors. Pin
Gerry Schmitz29-Jul-20 9:08
mveGerry Schmitz29-Jul-20 9:08 
GeneralRe: Do you all use wysiwyg web site editors. Pin
Jacquers29-Jul-20 21:24
Jacquers29-Jul-20 21:24 
GeneralRe: Do you all use wysiwyg web site editors. Pin
GenJerDan29-Jul-20 21:43
GenJerDan29-Jul-20 21:43 
GeneralRe: Do you all use wysiwyg web site editors. Pin
DumpsterJuice29-Jul-20 23:54
DumpsterJuice29-Jul-20 23:54 
GeneralRe: Do you all use wysiwyg web site editors. Pin
Ron Anders30-Jul-20 6:03
Ron Anders30-Jul-20 6:03 

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.