Store Lambda expressions as variable
up vote
2
down vote
favorite
Is it possible to store a lambda expression as a variable and use it in multiple places.
My db objects have an Id as int and a UId as an uniqueidentifier and I have to write very similar expressions when selecting based on Id or UId.
Lambda:
var result = await this.Worker.GetRepo<Category>().DbSet
.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules)
.SingleAsync(cat => cat.Id.Equals(id));
Is it possible to:
var expression = .Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
then use the variable like:
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.Id.Equals(id));
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.UId.Equals(uid));
I know the syntax is wrong, it's just what I'm looking for.
c# entity-framework lambda entity-framework-core
add a comment |
up vote
2
down vote
favorite
Is it possible to store a lambda expression as a variable and use it in multiple places.
My db objects have an Id as int and a UId as an uniqueidentifier and I have to write very similar expressions when selecting based on Id or UId.
Lambda:
var result = await this.Worker.GetRepo<Category>().DbSet
.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules)
.SingleAsync(cat => cat.Id.Equals(id));
Is it possible to:
var expression = .Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
then use the variable like:
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.Id.Equals(id));
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.UId.Equals(uid));
I know the syntax is wrong, it's just what I'm looking for.
c# entity-framework lambda entity-framework-core
You may want to look atFunc<T, TResult>
(msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx) which should allow you to do what you want.
– Chris
Nov 19 '17 at 1:16
1
@Chris, Or sometimesExpression<>
, that isExpression<Func<T, TResult>>
, since some flavors of Linq need an expression tree, to be translated into e.g. SQL, and cannot use a plain .NET delegate instance.
– Jeppe Stig Nielsen
Nov 11 at 11:17
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Is it possible to store a lambda expression as a variable and use it in multiple places.
My db objects have an Id as int and a UId as an uniqueidentifier and I have to write very similar expressions when selecting based on Id or UId.
Lambda:
var result = await this.Worker.GetRepo<Category>().DbSet
.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules)
.SingleAsync(cat => cat.Id.Equals(id));
Is it possible to:
var expression = .Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
then use the variable like:
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.Id.Equals(id));
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.UId.Equals(uid));
I know the syntax is wrong, it's just what I'm looking for.
c# entity-framework lambda entity-framework-core
Is it possible to store a lambda expression as a variable and use it in multiple places.
My db objects have an Id as int and a UId as an uniqueidentifier and I have to write very similar expressions when selecting based on Id or UId.
Lambda:
var result = await this.Worker.GetRepo<Category>().DbSet
.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules)
.SingleAsync(cat => cat.Id.Equals(id));
Is it possible to:
var expression = .Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
then use the variable like:
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.Id.Equals(id));
await this.Worker.GetRepo<Category>().expression.SingleAsync(cat => cat.UId.Equals(uid));
I know the syntax is wrong, it's just what I'm looking for.
c# entity-framework lambda entity-framework-core
c# entity-framework lambda entity-framework-core
asked Nov 19 '17 at 0:58
TheRuler
404219
404219
You may want to look atFunc<T, TResult>
(msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx) which should allow you to do what you want.
– Chris
Nov 19 '17 at 1:16
1
@Chris, Or sometimesExpression<>
, that isExpression<Func<T, TResult>>
, since some flavors of Linq need an expression tree, to be translated into e.g. SQL, and cannot use a plain .NET delegate instance.
– Jeppe Stig Nielsen
Nov 11 at 11:17
add a comment |
You may want to look atFunc<T, TResult>
(msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx) which should allow you to do what you want.
– Chris
Nov 19 '17 at 1:16
1
@Chris, Or sometimesExpression<>
, that isExpression<Func<T, TResult>>
, since some flavors of Linq need an expression tree, to be translated into e.g. SQL, and cannot use a plain .NET delegate instance.
– Jeppe Stig Nielsen
Nov 11 at 11:17
You may want to look at
Func<T, TResult>
(msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx) which should allow you to do what you want.– Chris
Nov 19 '17 at 1:16
You may want to look at
Func<T, TResult>
(msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx) which should allow you to do what you want.– Chris
Nov 19 '17 at 1:16
1
1
@Chris, Or sometimes
Expression<>
, that is Expression<Func<T, TResult>>
, since some flavors of Linq need an expression tree, to be translated into e.g. SQL, and cannot use a plain .NET delegate instance.– Jeppe Stig Nielsen
Nov 11 at 11:17
@Chris, Or sometimes
Expression<>
, that is Expression<Func<T, TResult>>
, since some flavors of Linq need an expression tree, to be translated into e.g. SQL, and cannot use a plain .NET delegate instance.– Jeppe Stig Nielsen
Nov 11 at 11:17
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You can just create a method that returns an IQueryable<Category>
. If you want the usage to be the same as your example then this could be an extension method:
public static IQueryable<Category> GetExpression(this IQueryable<Category> qry)
{
var expression = qry.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
return expression;
}
You can then use this as follows:
await this.Worker
.GetRepo<Category>()
.GetExpression()
.SingleAsync(cat => cat.UId.Equals(uid));
1
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You can just create a method that returns an IQueryable<Category>
. If you want the usage to be the same as your example then this could be an extension method:
public static IQueryable<Category> GetExpression(this IQueryable<Category> qry)
{
var expression = qry.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
return expression;
}
You can then use this as follows:
await this.Worker
.GetRepo<Category>()
.GetExpression()
.SingleAsync(cat => cat.UId.Equals(uid));
1
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
add a comment |
up vote
3
down vote
accepted
You can just create a method that returns an IQueryable<Category>
. If you want the usage to be the same as your example then this could be an extension method:
public static IQueryable<Category> GetExpression(this IQueryable<Category> qry)
{
var expression = qry.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
return expression;
}
You can then use this as follows:
await this.Worker
.GetRepo<Category>()
.GetExpression()
.SingleAsync(cat => cat.UId.Equals(uid));
1
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You can just create a method that returns an IQueryable<Category>
. If you want the usage to be the same as your example then this could be an extension method:
public static IQueryable<Category> GetExpression(this IQueryable<Category> qry)
{
var expression = qry.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
return expression;
}
You can then use this as follows:
await this.Worker
.GetRepo<Category>()
.GetExpression()
.SingleAsync(cat => cat.UId.Equals(uid));
You can just create a method that returns an IQueryable<Category>
. If you want the usage to be the same as your example then this could be an extension method:
public static IQueryable<Category> GetExpression(this IQueryable<Category> qry)
{
var expression = qry.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(cat => cat.GraphicItems)
.ThenInclude(gfx => gfx.Graphic)
.ThenInclude(gfx => gfx.Items)
.Include(m => m.Modules);
return expression;
}
You can then use this as follows:
await this.Worker
.GetRepo<Category>()
.GetExpression()
.SingleAsync(cat => cat.UId.Equals(uid));
edited Nov 11 at 11:08
answered Nov 19 '17 at 1:18
CalC
4,31831833
4,31831833
1
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
add a comment |
1
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
1
1
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
Great, thank you!
– TheRuler
Nov 19 '17 at 1:22
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f47372713%2fstore-lambda-expressions-as-variable%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
You may want to look at
Func<T, TResult>
(msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx) which should allow you to do what you want.– Chris
Nov 19 '17 at 1:16
1
@Chris, Or sometimes
Expression<>
, that isExpression<Func<T, TResult>>
, since some flavors of Linq need an expression tree, to be translated into e.g. SQL, and cannot use a plain .NET delegate instance.– Jeppe Stig Nielsen
Nov 11 at 11:17