使用Entity Framework的不同表

时间:2017-08-17 17:22:30

标签: asp.net-mvc entity-framework

我有一张这样的表:

+----+-------+------------------+
| ID | Name  | TipoContenedorID |
+----+-------+------------------+
|  1 | first |               10 |
|  2 | two   |                9 |
|  3 | three |               10 |
+----+-------+------------------+

因此,根据"TipoContenedorID",我得到另一个具有匿名类型的表的名称,如下所示:

 var vehiculo = _pService.Listar(x => x.TipoContenedor.CatalogosID.Equals("TCO"), includeProperties: "TipoContenedor").Select(x => new
                {
                    x.TipoContenedor.ID,
                    x.TipoContenedor.Nombre
                });

问题是我只想一次获得价值。其实我得到了:

TipoContenedor.Nombre = firstname
TipoContenedor.Nombre= secondname
TipoContenedor.Nombre = firstname

所以我两次得到第一个名字。如果重复传递它,我如何区分TipoContenedorID?此致

1 个答案:

答案 0 :(得分:2)

添加.Distinct()

var vehiculo = _pService
    .Listar(x => x.TipoContenedor.CatalogosID.Equals("TCO"), includeProperties: "TipoContenedor")
    .Select(x => new
            {
                x.TipoContenedor.ID,
                x.TipoContenedor.Nombre
            })
    .Distinct();