Making a column reference in a linq query dynamic

Posts   
 
    
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 17-Jun-2010 06:55:25   

If you could please look at the method I am trying to write below. Is there someway I can replace the "l.DeploymentId" part with something I could pass in as an argument to the method? Like and expression<func>...? Anything?


IQueryable<TEntity> SecureList<TEntity>(IServiceQuery query, IQueryable<TEntity> list, LinqMetaData meta, string entityType) where TEntity : SD.LLBLGen.Pro.ORMSupportClasses.IEntity2
        {
            if (!string.IsNullOrEmpty(query.RequiredPermission))
            {
                if (!UserId.HasValue)
                    return Enumerable.Empty<TEntity>().AsQueryable();

                list = from l in list
                        join c in meta.EntityRole on new { EntityId = l.DeploymentId, EntityType = entityType, UserId = UserId.Value } equals new { c.EntityId, c.EntityType, c.UserId }
                        join rp in meta.RolePermission on c.RoleId equals rp.RoleId
                        join p in meta.Permission on rp.PermissionId equals p.PermissionId
                        where p.Name == query.RequiredPermission
                        select l;
            }
        }

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 17-Jun-2010 09:52:24   

That's dynamic Linq.

Use Expression.Parameter(...) & Expression.Property(...)

All information and details can be found in the following links.

A very informative article: http://www.equivalence.co.uk/archives/819

Someone over here was trying the same thing, please check this thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=15424