IQueryable动态集属性

问题描述 投票:0回答:1

谁通过连接表达式设置属性

public interface IHiddenEntity
{
  bool Hidden { get; set;}
}
public interface IEntity 
{
   long Id { get; set;}
   string Name { get; set;}
}

public class Entity: IEntity, IHiddenEntity 
{
   public long Id { get; set;}
   public string Name { get; set;}
   public bool Hidden { get; set;}
}

public class Hidden 
{
   public string TableName {g et; set; }
   public long RecordId { get; set; }
}

public class Person: Entity{ ... }


public IQueryable<T> All(bool loadHidden = false)
  where T : class, IEntity, IHiddenEntity 
{
    string tableName = "<T> name";
    return from x in Context.Set<T>()
           join h in Context.Set<Hidden>().Where(record => record.TableName == tableName) on x.Id equals h.RecordId into hr
            from h_r in hr.DefaultIfEmpty()
            where loadHidden ? true : h_r == null
            select x;
}

但是我不明白如何设置 隐藏字段中的值。

  1. 仅需要返回IQueryable,因为仍然有方法执行的条件。
  2. 也不可能转换为IEnumerable:这不是最终值(请参见上文)。
entity-framework linq-to-entities iqueryable
1个回答
0
投票

您必须创建新实例:

public IQueryable<T> All(bool loadHidden = false)
  where T : class, new(), IEntity, IHiddenEntity 
{
    string tableName = "<T> name";
    return from x in Context.Set<T>()
           join h in Context.Set<Hidden>().Where(record => record.TableName == tableName) on x.Id equals h.RecordId into hr
           from h_r in hr.DefaultIfEmpty()
           where loadHidden ? true : h_r == null
           select new T
           {
               Hidden = loadHidden,
               // other fields based on x
           };
}

也可以进行表达式操作,here是其中的一个示例

© www.soinside.com 2019 - 2024. All rights reserved.