我有一个我不想改变的实体方法:
Listar方法
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>();
}
}
我使用像
这样的查询var vehiculo = _pService.Listar(x => x.TipoContenedor.CatalogosID.Equals("TCO"), includeProperties: "TipoContenedor").Select(x => x.TipoContenedor.Nombre);
表格
public class TipoVehiculo: Entidad<Int32>
{
[StringLength(255)]
public string Nombre { get; set; }
[StringLength(255)]
public string CapacidadCarga { get; set; }
[StringLength(255)]
public string CapacidadCubica { get; set; }
public int TipoContenedorID { get; set; }
public CatalogoRegistro TipoContenedor { get; set; }
public int EstatusID { get; set; }
public Estatus Estatus { get; set; }
}
问题是当我想使用select .Select(x => x.TipoContenedor.Nombre)
时,因为当我尝试使用mapper的查询时:
var vehiculo = _pService.Listar(x => x.TipoContenedor.CatalogosID.Equals("TCO"), includeProperties: "TipoContenedor").Select(x => x.TipoContenedor.Nombre);
IEnumerable<TipoVehiculoViewModel> est = Mapper.Map<IEnumerable<TipoVehiculo>, IEnumerable<TipoVehiculoViewModel>>(vehiculo);
response = request.CreateResponse<IEnumerable<TipoVehiculoViewModel>>(HttpStatusCode.OK, est);
我在vehiculo中收到错误说:
参数类型System.Collection.Generic.IEnumerable不是 可分配给参数类型 System.Collections.Generic.IEnumerable
如果只发送INNERJOIN
的表格列,我需要做什么?
注意:那个innerjoin是因为我使用了TipoContenedor的include属性,所以EF将它解释为与该表的内连接。
查询
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Nombre] AS [Nombre],
[Extent1].[CapacidadCarga] AS [CapacidadCarga],
[Extent1].[CapacidadCubica] AS [CapacidadCubica],
[Extent1].[TipoContenedorID] AS [TipoContenedorID],
[Extent1].[EstatusID] AS [EstatusID],
[Extent2].[ID] AS [ID1],
[Extent2].[Codigo] AS [Codigo],
[Extent2].[Nombre] AS [Nombre1],
[Extent2].[PadreID] AS [PadreID],
[Extent2].[CatalogosID] AS [CatalogosID],
[Extent2].[FechaCreacion] AS [FechaCreacion],
[Extent2].[UsuarioCreacionID] AS [UsuarioCreacionID],
[Extent2].[FechaModificacion] AS [FechaModificacion],
[Extent2].[UsuarioModificacionID] AS [UsuarioModificacionID],
[Extent2].[EstatusRegistro] AS [EstatusRegistro]
FROM [adm].[TiposVehiculo] AS [Extent1]
INNER JOIN [adm].[Catalogos_Registros] AS [Extent2] ON [Extent1].[TipoContenedorID] = [Extent2].[ID]
WHERE N'TCO' = [Extent2].[CatalogosID]
我想要的列是[Extent2].[Nombre] AS [Nombre1]
,来自innerjoin子句