简体   繁体   English

使用linq join正确填充IQueryable模型

[英]Fill correctly a IQueryable model with linq join

I have to show data on a listview item, so I have the following code behind: 我必须在listview项上显示数据,所以后面有以下代码:

public IQueryable mostrarMesas()
{
    var query = from area in db.res_areas_sucursal
                join mesa in db.res_mesas_sucursal on area.area_gkey equals mesa.area_gkey
                select mesa;

    return query;
}

This joins two tables data and returns the data I need, but I need to show data from both tables, I tried creating this model that I think has what I need 这将连接两个表数据并返回我需要的数据,但是我需要显示两个表中的数据,我尝试创建这个我认为自己有需要的模型

public class Mesas
{
    [Key]
    public long? mesa_gkey { get; set; }

    public String descripcion_mesa { get; set; }

    public res_areas_sucursal res_areas_sucursal { get; set; }

    public int total_capacidad { get; set; }

    public bool disponible_para_reserva { get; set; }
}

But I don't know how to assign the query to return a list of Mesas objects instead of the ones from the entity framework. 但是我不知道如何分配查询以返回Mesas对象列表,而不是实体框架中的对象。 I'm using ASP.NET Webforms 4.5 我正在使用ASP.NET Webforms 4.5

How could I do this? 我该怎么办?

Should I establish the relations on the database first? 我应该首先在数据库上建立关系吗?

You can use anonymous types: 您可以使用匿名类型:

var query = from area in db.res_areas_sucursal
            join mesa in db.res_mesas_sucursal on area.area_gkey equals mesa.area_gkey
            select new { area, mesa } ;
            return query;

Or you can use your model: 或者您可以使用模型:

var query = from area in db.res_areas_sucursal
                join mesa in db.res_mesas_sucursal on area.area_gkey equals mesa.area_gkey
                select new Mesas()
                {
                   mesa_gkey = mesa.gkey,
                   // continue the rest
                };

                return query;

Inside the select, you can use both area and mesa . 在选择内,您可以同时使用areamesa Then you can select whatever properties (columns) you need and assign them. 然后,您可以选择所需的任何属性(列)并进行分配。

Note that it is better to return IQueryable<Mesas> instead of just IQueryable unless you have a valid reason not to do so. 需要注意的是,最好是返回IQueryable<Mesas>而不是仅仅的IQueryable ,除非你有正当理由不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM