从连接的linq语句中选择视图模型列表

时间:2015-08-20 02:31:15

标签: c# linq

我在linq声明中加入了一个简单的

var events = (from e in context.Events
join o in context.Offices on e.OfficeId equals o.Id
where e.StartDate >= DateTime.Now
select e)
.AsEnumerable()
.Select(x => new vm_Events()
{
    Cost = x.Cost,
    StartDate = x.StartDate,
    EndDate = x.EndDate.HasValue ? (x.EndDate).Value : new DateTime(),
    Address = x.Address,
    Image = x.SupportingImage,
    Info = x.Info,
    Name = x.Name,
    LocationName = x.LocationName,
    Office =
        string.Format("For more information please call the {0} office at {1}", x.Offices.Name,
            x.Offices.PhoneNumber),
    StateZip = string.Format("{0}, {1}, {2}", x.City, x.State, x.Zip)
}).ToList();

查询构建得很好,但是当我包含连接的offices表时,我会给出空引用。在select e来电之前,是否需要将.AsEnumerable()更改为其他任何内容?

我已经确认我在办公室桌上寻找的数据不为空

1 个答案:

答案 0 :(得分:1)

如果您没有延迟加载,则无法在 <asset:javascript src="application.js"/> 之后访问//= require bootstrap 。我建议你做两个步骤:

  1. 在匿名类中加载您需要的所有数据
  2. 构建vm_Events对象
  3. x.Offices.Name