【发布时间】:2020-09-17 23:24:32
【问题描述】:
谁从连接表达式中设置属性
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;
}
但我不明白如何设置 隐藏字段中的值。
- 只需要返回 IQueryable,因为还有 方法执行的条件。
- 也不可能 转换为 IEnumerable:这不是最终值(见上文)。
UDP: 没有考虑为每个模型创建选择的选项 - 许多模型具有大量字段!
【问题讨论】:
-
你为什么有这个
Hidden实体/表?设置这些实体的过程也可以直接在适当的实体中设置Hidden属性。
标签: entity-framework linq-to-entities iqueryable