【问题标题】:LinqPad Query to Visual Studio - how to use a nested query to populate a viewmodelLinqPad Query to Visual Studio - 如何使用嵌套查询来填充视图模型
【发布时间】:2012-08-13 05:08:27
【问题描述】:

这是对先前问题的后续处理。

我想填充一个 ViewModel,它有 3 个属性和一个 Occ 类列表(也有 3 个属性。

public class RatesViewModel
{
    public string TypeName { get; set; }
    public long TypeID { get; set; }
    public int TypeCount { get; set; }
    public virtual IQueryable<Occ> Occs { get; set; }
}

public class Occ
{
    public string occ { get; set; }
    public decimal ratetocharge { get; set; }
    public int numOfOcc { get; set; }
    public virtual RatesViewModel RatesViewModel { get; set; }
}

当我在 LinqPad 中运行以下 Linq 查询时:

var rooms = tblRoom
    .GroupBy(p => p.tblType)
    .Select(g => new
        {
        TypeName = g.Key.type_name,
        TypeID = g.Key.type_id,
        TypeCount = g.Count(),
          Occs = rates.Where(rt => rt.type_id == g.Key.type_id && 
          (
            (rt.type_id == g.Key.type_id)
          ))
          .GroupBy(rt => rt.occ)
          .Select(proj => new 
                       {
                          occ = proj.Key,
                          ratetocharge = proj.Sum(s => s.rate),
                           numOfOcc = proj.Count()
                       })
        });

        rooms.Dump();

...和以前一样,它正确返回了我正在寻找的数据模型:

...当我点击 Occs 时,它会深入到 Occs 类:

LinqPad 中的完整视图是:

我在 Visual Studio 中的查询是:

    var rooms = dbr.Rooms
                .GroupBy(p => p.RoomTypes).Select(g => new RatesViewModel
                {
                    TypeName = g.Key.type_name,
                    TypeID = g.Key.type_id,
                    TypeCount = g.Count()
                    ,
                  Occs = db.Rates.Where(rt => rt.type_id == g.Key.type_id && 
                       (
                        (rt.type_id == g.Key.type_id)
                       ))
                      .GroupBy(rt => rt.occ)
                      .Select(proj => new Occ
                       {
                          occ = proj.Key,
                          ratetocharge = proj.Sum(s => s.rate),
                           numOfOcc = proj.Count()
                       })
                })
                .ToList();

但是当运行这个时,我得到一个错误:

指定的 LINQ 表达式包含对与不同上下文关联的查询的引用。

我想我理解了这个错误 - 但我不确定如何将查询分成 2 个单独的查询,然后再次将这些查询结果连接在一起以获得我的原始结果集。

我的模型类是:

public class Rates
{
    public int id { get; set; }
    public long type_id { get; set; }
    public DateTime ratedate { get; set; }
    public decimal rate { get; set; }
    public string occ { get; set; }
    public List<RoomType> Type { get; set; }
}

public class Rental
    {
        [Key]
        public long rental_id { get; set; }
        public long room_id { get; set; }
        public DateTime check_in { get; set; }
        public DateTime check_out { get; set; }
        public virtual Room Room { get; set; }
}

public class Room
{
    [Key]
    public long room_id { get; set; }
    public long type_id { get; set; }
    public virtual RoomType RoomTypes { get; set; }
    public virtual ICollection<Rental> Rentals { get; set; }
}

public class RoomType
{
    [Key]
    public long type_id { get; set; }
    public string type_name { get; set; }
    public IQueryable<Rates> Rates { get; set; }
    public virtual ICollection<Room> Room { get; set; }
}

谁能帮助检查我的查询或模型,使其适用于一个查询,或者告诉我如何将查询分成两个,然后合并结果集?

谢谢,

标记

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 linq entity-framework


    【解决方案1】:

    apitest.Models.RoomContext' 不包含“Rates”的定义...

    (您对 hydr 答案的评论)

    好吧,你去吧:不仅是两个不同的上下文instances,还有两个不同的上下文。我怀疑您的 linqpad 查询直接针对数据库连接,这意味着它使用了一个 linq-to-sql DataContext(动态创建)。

    您需要在查询中使用一个上下文类(及其一个实例)。并在 Linqpad 中连接到它,以确保您测试与 Visual Studio 相同的查询提供程序。

    【讨论】:

      【解决方案2】:

      dbr 和 db 似乎是同一上下文的两个不同实例。但是在一个查询中,您应该只使用一个上下文。所以我建议如下:

      Occs = dbr.Rates.Where(rt => rt.type_id == g.Key.type_id && ....
      

      如果这没有帮助,您能否引用初始化上下文的行?

      【讨论】:

      • 嗨 - 如果我更改为 dbr。 - 我收到错误:错误 4 'apitest.Models.RoomContext' 不包含 'Rates' 的定义,并且找不到接受类型为 'apitest.Models.RoomContext' 的第一个参数的扩展方法 'Rates'(你是缺少 using 指令或程序集引用?)我在控制器的开头初始化上下文: private RatesContext db = new RatesContext();私人 RoomContext dbr = new RoomContext();私人 RoomTypeContext dbt = new RoomTypeContext();
      • 我是否需要构建我的域模型,以便它们都相互引用 - 然后允许我使用一个上下文(因此不必分离查询)?谢谢@hydr
      猜你喜欢
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2023-03-12
      • 2019-08-10
      • 1970-01-01
      • 2012-04-23
      相关资源
      最近更新 更多