How to create dynamic lambda join query using expression in C#?
I am new to dynamic expression query...
I want to create a join query dynamically. Below it the query for which I want the equivalent dynamic query:
var lstNums = new List<int> { 100, 101 };
var getAll = new StudenRepository().GetAll(); //Returns IQuerable<Student>
var query = getAll.Join(lstNums, a => a.StudentId, b => b, (a, b) => a).ToList();
- The
lstNums
can be list of any primitive data types - The
getAll
contains IQuerable, this can be IQuerable of any entity - The
query
will contain theList<Student>
records after doing join with thegetAll
and list of integers. But the result can be any list of entity. The list i.e. lstNum can be any list of primitive data types.
Below is what I have tried:
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression expnResult = Expression.Parameter(typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, expnResult.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), expnInput,
expnResult));
}
The extension method:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
Note: I have installed System.Linq.Dynamic
nuget package version 1.0.7
for dynamic linq expression.
c# lambda expression expression-trees
add a comment |
I am new to dynamic expression query...
I want to create a join query dynamically. Below it the query for which I want the equivalent dynamic query:
var lstNums = new List<int> { 100, 101 };
var getAll = new StudenRepository().GetAll(); //Returns IQuerable<Student>
var query = getAll.Join(lstNums, a => a.StudentId, b => b, (a, b) => a).ToList();
- The
lstNums
can be list of any primitive data types - The
getAll
contains IQuerable, this can be IQuerable of any entity - The
query
will contain theList<Student>
records after doing join with thegetAll
and list of integers. But the result can be any list of entity. The list i.e. lstNum can be any list of primitive data types.
Below is what I have tried:
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression expnResult = Expression.Parameter(typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, expnResult.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), expnInput,
expnResult));
}
The extension method:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
Note: I have installed System.Linq.Dynamic
nuget package version 1.0.7
for dynamic linq expression.
c# lambda expression expression-trees
add a comment |
I am new to dynamic expression query...
I want to create a join query dynamically. Below it the query for which I want the equivalent dynamic query:
var lstNums = new List<int> { 100, 101 };
var getAll = new StudenRepository().GetAll(); //Returns IQuerable<Student>
var query = getAll.Join(lstNums, a => a.StudentId, b => b, (a, b) => a).ToList();
- The
lstNums
can be list of any primitive data types - The
getAll
contains IQuerable, this can be IQuerable of any entity - The
query
will contain theList<Student>
records after doing join with thegetAll
and list of integers. But the result can be any list of entity. The list i.e. lstNum can be any list of primitive data types.
Below is what I have tried:
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression expnResult = Expression.Parameter(typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, expnResult.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), expnInput,
expnResult));
}
The extension method:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
Note: I have installed System.Linq.Dynamic
nuget package version 1.0.7
for dynamic linq expression.
c# lambda expression expression-trees
I am new to dynamic expression query...
I want to create a join query dynamically. Below it the query for which I want the equivalent dynamic query:
var lstNums = new List<int> { 100, 101 };
var getAll = new StudenRepository().GetAll(); //Returns IQuerable<Student>
var query = getAll.Join(lstNums, a => a.StudentId, b => b, (a, b) => a).ToList();
- The
lstNums
can be list of any primitive data types - The
getAll
contains IQuerable, this can be IQuerable of any entity - The
query
will contain theList<Student>
records after doing join with thegetAll
and list of integers. But the result can be any list of entity. The list i.e. lstNum can be any list of primitive data types.
Below is what I have tried:
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression expnResult = Expression.Parameter(typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, expnResult.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), expnInput,
expnResult));
}
The extension method:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
Note: I have installed System.Linq.Dynamic
nuget package version 1.0.7
for dynamic linq expression.
c# lambda expression expression-trees
c# lambda expression expression-trees
asked Dec 28 '18 at 10:50
Dukhabandhu SahooDukhabandhu Sahoo
72811438
72811438
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The below code worked for me:
using DynamicLinq = System.Linq.Dynamic;
using LinqExpression = System.Linq.Expressions;
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
if (innerEntities == null) throw new ArgumentNullException(nameof(innerEntities));
if (firstEntityPropName == null) throw new ArgumentNullException(nameof(firstEntityPropName));
if (typeSecondEntity == null) throw new ArgumentNullException(nameof(typeSecondEntity));
if (typeResultEntity == null) throw new ArgumentNullException(nameof(typeResultEntity));
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression parameters = new ParameterExpression {
Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(innerEntities.AsQueryable().ElementType, "inner")
};
LambdaExpression selectorSecondEntity = DynamicLinq.DynamicExpression.ParseLambda(new ParameterExpression { expnInput }, typeSecondEntity, "inner");
LambdaExpression selectorResult = DynamicLinq.DynamicExpression.ParseLambda(parameters, typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, selectorResult.Body.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(selectorSecondEntity),
Expression.Quote(selectorResult)));
}
The extension method is as follows:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957334%2fhow-to-create-dynamic-lambda-join-query-using-expression-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The below code worked for me:
using DynamicLinq = System.Linq.Dynamic;
using LinqExpression = System.Linq.Expressions;
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
if (innerEntities == null) throw new ArgumentNullException(nameof(innerEntities));
if (firstEntityPropName == null) throw new ArgumentNullException(nameof(firstEntityPropName));
if (typeSecondEntity == null) throw new ArgumentNullException(nameof(typeSecondEntity));
if (typeResultEntity == null) throw new ArgumentNullException(nameof(typeResultEntity));
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression parameters = new ParameterExpression {
Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(innerEntities.AsQueryable().ElementType, "inner")
};
LambdaExpression selectorSecondEntity = DynamicLinq.DynamicExpression.ParseLambda(new ParameterExpression { expnInput }, typeSecondEntity, "inner");
LambdaExpression selectorResult = DynamicLinq.DynamicExpression.ParseLambda(parameters, typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, selectorResult.Body.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(selectorSecondEntity),
Expression.Quote(selectorResult)));
}
The extension method is as follows:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
add a comment |
The below code worked for me:
using DynamicLinq = System.Linq.Dynamic;
using LinqExpression = System.Linq.Expressions;
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
if (innerEntities == null) throw new ArgumentNullException(nameof(innerEntities));
if (firstEntityPropName == null) throw new ArgumentNullException(nameof(firstEntityPropName));
if (typeSecondEntity == null) throw new ArgumentNullException(nameof(typeSecondEntity));
if (typeResultEntity == null) throw new ArgumentNullException(nameof(typeResultEntity));
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression parameters = new ParameterExpression {
Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(innerEntities.AsQueryable().ElementType, "inner")
};
LambdaExpression selectorSecondEntity = DynamicLinq.DynamicExpression.ParseLambda(new ParameterExpression { expnInput }, typeSecondEntity, "inner");
LambdaExpression selectorResult = DynamicLinq.DynamicExpression.ParseLambda(parameters, typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, selectorResult.Body.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(selectorSecondEntity),
Expression.Quote(selectorResult)));
}
The extension method is as follows:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
add a comment |
The below code worked for me:
using DynamicLinq = System.Linq.Dynamic;
using LinqExpression = System.Linq.Expressions;
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
if (innerEntities == null) throw new ArgumentNullException(nameof(innerEntities));
if (firstEntityPropName == null) throw new ArgumentNullException(nameof(firstEntityPropName));
if (typeSecondEntity == null) throw new ArgumentNullException(nameof(typeSecondEntity));
if (typeResultEntity == null) throw new ArgumentNullException(nameof(typeResultEntity));
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression parameters = new ParameterExpression {
Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(innerEntities.AsQueryable().ElementType, "inner")
};
LambdaExpression selectorSecondEntity = DynamicLinq.DynamicExpression.ParseLambda(new ParameterExpression { expnInput }, typeSecondEntity, "inner");
LambdaExpression selectorResult = DynamicLinq.DynamicExpression.ParseLambda(parameters, typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, selectorResult.Body.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(selectorSecondEntity),
Expression.Quote(selectorResult)));
}
The extension method is as follows:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
The below code worked for me:
using DynamicLinq = System.Linq.Dynamic;
using LinqExpression = System.Linq.Expressions;
public static IQueryable JoinQuery(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName,
Type typeSecondEntity, Type typeResultEntity, params object values)
{
if (innerEntities == null) throw new ArgumentNullException(nameof(innerEntities));
if (firstEntityPropName == null) throw new ArgumentNullException(nameof(firstEntityPropName));
if (typeSecondEntity == null) throw new ArgumentNullException(nameof(typeSecondEntity));
if (typeResultEntity == null) throw new ArgumentNullException(nameof(typeResultEntity));
LambdaExpression outerSelectorLambda = DynamicLinq.DynamicExpression.ParseLambda(outer.ElementType, null, firstEntityPropName, values);
ParameterExpression expnInput = Expression.Parameter(typeSecondEntity, "inner");
ParameterExpression parameters = new ParameterExpression {
Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(innerEntities.AsQueryable().ElementType, "inner")
};
LambdaExpression selectorSecondEntity = DynamicLinq.DynamicExpression.ParseLambda(new ParameterExpression { expnInput }, typeSecondEntity, "inner");
LambdaExpression selectorResult = DynamicLinq.DynamicExpression.ParseLambda(parameters, typeResultEntity, "outer");
return outer.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Join",
new Type { outer.ElementType, innerEntities.AsQueryable().ElementType, outerSelectorLambda.Body.Type, selectorResult.Body.Type },
outer.Expression, innerEntities.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(selectorSecondEntity),
Expression.Quote(selectorResult)));
}
The extension method is as follows:
public static IQueryable<T> JoinQuery<T>(this IQueryable outer, IEnumerable innerEntities, string firstEntityPropName, Type typeSecondEntity, Type typeResultEntity, params object values)
{
return (IQueryable<T>)Extensions.JoinQuery((IQueryable)outer, (IEnumerable)innerEntities, firstEntityPropName, typeSecondEntity, typeResultEntity, values);
}
answered Dec 31 '18 at 4:30
Dukhabandhu SahooDukhabandhu Sahoo
72811438
72811438
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957334%2fhow-to-create-dynamic-lambda-join-query-using-expression-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown