我有两张桌子
Estatus表
+----+----------+
| ID | Nombre |
+----+----------+
| 16 | ACTIVE |
| 2 | DISABLED |
+----+----------+
TipoCarga表
+----+----------+--------------------+-----------+
| ID | Nombre | Descripcion | EstatusID |
+----+----------+--------------------+-----------+
| 1 | Active | first descriptio | 16 |
| 2 | Disabled | second description | 2 |
+----+----------+--------------------+-----------+
我有自定义实体列出并收费如下:
IEnumerable<T> Listar(Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = "");
T Cargar(Expression<Func<T, bool>> filter,
string includeProperties = "");
如果我做的话
var claseid = _claseService.Cargar(c => c.Nombre.Equals("TIPOS DE CARGA")).ID;
var estatusid = _estatusService.Cargar(e => e.Nombre.Equals("ACTIVE") && e.ClaseID.Equals(claseid)).ID;
var tc = _tipoCargaService.Listar(c => c.EstatusID.Equals(estatusid), includeProperties: "Estatus");
它会加载值,但只会加载Nombre.Equals("ACTIVO")
的值,并且我希望获得所有Nombre
值
首先,我尝试获取所有Estatus的列表:
//I don´t need this line because I dont wannt to filte by clase
//var claseid = _claseService.Cargar(c => c.Nombre.Equals("TIPOS DE CARGA")).ID;
//get list of all estatus
var estatusid = _estatusService.Listar();
然后使用此列表并使用根据表TipoCarga的EstatusID检索值“Nombre”,如:
var tc = _tipoCargaService.Listar(c => c.EstatusID.Equals(estatusid), includeProperties: "Estatus");
问题是var estatusid = _estatusService.Listar();
得到0值,我不知道为什么它没有得到价值,有人能帮忙吗?此致
方法实施:
public virtual IEnumerable<T> Listar(
Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = "")
{
IQueryable<T> query = _dbset;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).AsEnumerable<T>();
}
else
{
return query.AsEnumerable<T>();
}
}
public virtual T Cargar(
Expression<Func<T, bool>> filter,
string includeProperties = "")
{
IQueryable<T> query = _dbset;
query = query.AsNoTracking().Where(filter);
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
return query.FirstOrDefault();
}
更新
如果我使用.Tolist
作为 Ivan Stoev 注释,则返回Estatus的所有值,但现在传递给:
var tc = _tipoCargaService.Listar(c => c.EstatusID.Equals(estatusid), includeProperties: "Estatus");
然后它加载foreach像:
if (tc != null)
{
List<TipoCargaViewModel> tiposcargas = new List<TipoCargaViewModel>();
foreach (var item in tc)
{
var carga = new TipoCargaViewModel()
{
ID = item.ID,
Nombre = item.Nombre,
Descripcion = item.Descripcion,
EstatusID = item.EstatusID,
NombreEstatus = item.Estatus.Nombre
};
tiposcargas.Add(carga);
}
但是当它试图传递foreach时会引发异常:
无法创建“Project.Model.Estatus”类型的常量值。 此处仅支持原始类型或枚举类型 上下文。