1

下午好,

我在延迟加载空子对象时遇到问题。考虑以下代码:

// Retrieve all repairs
var repairs = this.db.Repairs.Include("Brand").Include("Model");

// JUST FOR TESTING PURPOSES
// Some repairs do not have a brand or model. This query returns about 14 results
var reps = this.db.Repairs.Where(r => r.ModelId == 0 || r.BrandId == 0);

// Map the repairs to the viewmodel
var ret = repairs.Select(qRepair => new RepairListViewModel
{
    Brand = qRepair.Brand == null ? "Unkown" : qRepair.Brand.Name,
    CompanyName = qRepair.User.CompanyName,
    CustomerName = qRepair.CustomerName,
    Created = qRepair.Created,
    DamageNumber = qRepair.DamageNumber,
    Id = qRepair.Id,
    Model = qRepair.Model == null ? "Unkown" : qRepair.Model.Name,
    OrderNumber = qRepair.Order.OrderNumber,
    Printed = qRepair.Printed,
    Status = qRepair.Status
});

在此之后,返回列表以供在 Kendo UI Grid 中使用。

但是,品牌或型号名称为“Unkown”的产品没有维修。它似乎跳过了品牌或型号为空的维修。我在这里想念什么?!

提前致谢!

4

1 回答 1

0

我会使用这个语法来解决这个问题:

var query = from pro in db.Repais
      select new {
                   Brand = qRepair.Brand == null ? "Unkown" : qRepair.Brand.Name,
                   CompanyName = qRepair.User.CompanyName,
                   CustomerName = qRepair.CustomerName,
                   Created = qRepair.Created,
                   DamageNumber = qRepair.DamageNumber,
                   Id = qRepair.Id,
                   Model = qRepair.Model == null ? "Unkown" : qRepair.Model.Name,
                   OrderNumber = qRepair.Order.OrderNumber,
                   Printed = qRepair.Printed,
                   Status = qRepair.Status
                  };

return query.ToList<Object>();

我不能说你的代码到底有什么问题。您可以尝试在 VS 中加载 Linq dll 并尝试调试以查找一些错误信息。不要忘记包含。但我认为问题在于数据上下文中的 reutrn 类型。

我希望这会帮助你。

于 2013-08-30T15:15:13.993 回答