0

我在这里读了一些问题,但它们都可以定义为单独的变量,但我认为我的有点不同:

  var sliderRecordList = this._sliderService.GetAllAsQueryable();      
  var sliderModelList = sliderRecordList.Select(record => new SliderModel()
      {
        Id = record.Id,
        SlideName = record.SlideName,
        SlideOrder = record.SlideOrder,
        SlideUrl = record.SlideUrl,
        SlideImageUrl = Url.Content("~/Content/AhsenSliderImages/" + record.Id + ".jpg"),
        Enabled = record.Enabled
   });

问题取决于Url.Content()调用方法的位置。有什么建议吗?我试图删除Url.Content()方法但没有工作。

4

2 回答 2

2

首先物化查询,然后设置 UI 关注点。

var sliderRecordList = this._sliderService.GetAllAsQueryable().ToList();
var sliderModelList = sliderRecordList
   .Select(record => new SliderModel
      {
        Id = record.Id,
        SlideName = record.SlideName,
        SlideOrder = record.SlideOrder,
        SlideUrl = record.SlideUrl,
        SlideImageUrl = Url.Content("~/Content/AhsenSliderImages/" + record.Id + ".jpg"),
        Enabled = record.Enabled
   });

.ToList()将首先执行查询。

附带说明一下,考虑使用AutoMapper将上面的代码变成几行代码:

var sliderRecordList = this._sliderService.GetAllAsQueryable().ToList();
var sliderModelList = Mapper.Map<ICollection<SliderRecord>, ICollection<SliderModel>>(sliderRecordList);

URL 生成可以在视图中完成。

于 2012-10-04T01:31:48.630 回答
1

这是因为该函数Url.Content无法转换为其 SQL 等效函数。对列表使用AsEnumerable函数并尝试一下

var sliderModelList = sliderRecordList.AsEnumerable.Select(record => new SliderModel()
      {
        Id = record.Id,
        SlideName = record.SlideName,
        SlideOrder = record.SlideOrder,
        SlideUrl = record.SlideUrl,
        SlideImageUrl = Url.Content("~/Content/AhsenSliderImages/" + record.Id + ".jpg"),
        Enabled = record.Enabled
   });

祝你好运。

于 2012-10-04T01:40:04.753 回答