Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用实体值填充DTO模型_C#_Entity Framework - Fatal编程技术网

C# 用实体值填充DTO模型

C# 用实体值填充DTO模型,c#,entity-framework,C#,Entity Framework,我有以下DTO: public class EbookDTO { public Int32 EbookId { get; set; } public Int32 CoverId { get; set; } public Int32 DocumentId { get; set; } } 我有以下实体框架实体: public class EbookFile { public Int32 EbookId { get; set; } public Int32 FileId { ge

我有以下DTO:

public class EbookDTO {
  public Int32 EbookId { get; set; }
  public Int32 CoverId { get; set; }
  public Int32 DocumentId { get; set; }
}
我有以下实体框架实体:

public class EbookFile {
  public Int32 EbookId { get; set; }
  public Int32 FileId { get; set; }

  public virtual Ebook Ebook { get; set; }
  public virtual File File { get; set; }
}

public class File {
  public Int32 Id { get; set; }
  public Int32 EbookFileId { get; set; }
  public String Name { get; set; }
  public virtual EbookFile EbookFile { get; set; }
}
我有以下
EbookDTO
列表:

List<EbookDTO> ebooks = new List<EbookDTO>() {
  new EbookDTO { Id = 1 },
  new EbookDTO { Id = 2 }
}

但是我不知道如何测试文件名,获取其id并添加定义每个
EbookDTO

DocumentId
CoverId
,从
EbookDTO
开始查询会使这变得非常困难。一本
电子书有多个
电子书文件
,因此您需要某种形式的分组来收集构建一本
电子书所需的两条记录

如果从
电子书
开始查询,则自然会提供此分组:

var ebookIds = ebooks.Select(y => y.Id).ToArray();

from eb in context.Ebooks
where ebookIds.Contains(eb.EbookId)
select new EbookDTO
{
    EbookId = eb.EbookId,
    CoverId = eb.EbookFiles.FirstOrDefault(ebf => ebf.File.Name == "Cover").FileId,
    DocumentId = eb.EbookFiles.FirstOrDefault(ebf => ebf.File.Name == "Document").FileId,
}
我假设此导航属性
EbookFiles
存在于
Ebook
中,否则我建议创建它

var ebookIds = ebooks.Select(y => y.Id).ToArray();

from eb in context.Ebooks
where ebookIds.Contains(eb.EbookId)
select new EbookDTO
{
    EbookId = eb.EbookId,
    CoverId = eb.EbookFiles.FirstOrDefault(ebf => ebf.File.Name == "Cover").FileId,
    DocumentId = eb.EbookFiles.FirstOrDefault(ebf => ebf.File.Name == "Document").FileId,
}