谁通过连接表达式设置属性
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;
}
但是我不明白如何设置 隐藏字段中的值。
您必须创建新实例:
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是其中的一个示例