【问题标题】:EntityFramework. SelectMany with Anonymous Type and Projection实体框架。具有匿名类型和投影的 SelectMany
【发布时间】:2013-03-01 13:03:26
【问题描述】:

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

我有以下疑问:

  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 个项目的列表。满足条件的那个。

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

谢谢你, 米格尔

【问题讨论】:

    标签: entity-framework


    【解决方案1】:

    这应该是:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      相关资源
      最近更新 更多