0

我有一个带有多个包的横幅。每个包有多个文件。

我有以下查询:

  List<BannerModel> models = context.Banners
    .Select(x => x.Packs
      .SelectMany(p => p.Files, (p, f) => new {
        Id = p.Id,
        Flag = p.Flag,
        File = new { Id = f.Id, Flag = f.Flag, Key = f.Key, Mime = f.Mime }
      })
      .Where(a => a.File.Flag == "Img_200")
      .Select(a => new BannerModel { PackId = a.Id, ImageKey = a.File.Key })
    ).ToList();

1) 我在“ToList()”上得到错误。无法将类型“System.Collections.Generic.List>”隐式转换为“System.Collections.Generic.List”

2)然后我删除了 ToList 并添加了“var models = ...”

我知道有 10 条记录,其中 5 条满足条件:

.Where(a => a.File.Flag == "Img_200")

奇怪的是我得到了 10 个项目,5 个有数据,5 个没有数据。

我应该只得到一个包含 5 个项目的列表。满足条件的那个。

有人可以帮我解决这个问题吗?

谢谢你,米格尔

4

1 回答 1

2

这应该是:

List<BannerModel> models = context.Banners
    .SelectMany(x => x.Packs
        .SelectMany(p => p.Files, (p, f) => new {
            Id = p.Id,
            Flag = p.Flag,
            File = new { Id = f.Id, Flag = f.Flag, Key = f.Key, Mime = f.Mime }
        })
    .Where(a => a.File.Flag == "Img_200")
    .Select(a => new BannerModel { PackId = a.Id, ImageKey = a.File.Key })
    ).ToList();
于 2013-03-01T14:05:12.077 回答