2

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.

2
  • 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. Commented Nov 19, 2017 at 1:16
  • 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. Commented Nov 11, 2018 at 11:17

1 Answer 1

5

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.