1

I have models that look like the following:

public class Note
{
    public long? ID { get; set; }
    public long AuthorID { get; set; }
    public virtual ICollection<Image> Images { get; set; }
    ...
}

public class Image
{
    public long? ID { get; set; }
    public byte[] Thumbnail { get; set; }
    public byte[] FullRes { get; set; }
}

Which I can load with the following:

List<Note> a = dBContext.Notes.Where(x => x.AuthorID == myId)
        .Include(x => x.Images).ToList();

I would however like to avoid loading the FullRes member of Image as it is large. Something along the lines of the following (which throws an ArgumentException - The include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.):

List<Note> a = dBContext.Notes.Where(x => x.AuthorID == myId)
        .Include(x => x.Images.Select(i => new Image
        {
            ID = i.ID,
            Thumbnail = i.Thumbnail
        })).ToList();

Does anyone know the correct syntax to achieve this goal?

denver
  • 2,863
  • 2
  • 31
  • 45

2 Answers2

1

I did not test it. You could try something like this.

var notes = dBContext.Notes.Where(x => x.AuthorID == myId)
.Select(x=> new {
    x.ID,
    x.AuthorID,
    Images = x.Images.Select(i=> new {
        i.ID,
        i.Thumbnail
    }).ToList()
}).ToList();

// Rewrite
List<Note> a = notes.Select(x=> new Note{
    x.ID,
    x.AuthorID,
    Images = x.Images.Select(i=> new Image{
        i.ID,
        i.Thumbnail
    }).ToList()
}).ToList();
Amit Hasan
  • 1,430
  • 1
  • 15
  • 33
  • Thanks for answering. As I commented on the similar an I tested the Select query and got a NotSupportedException: "The entity of complex type Note cannot be constructed in a LINQ to Entities query". – denver Jul 22 '20 at 15:22
  • @denver buddy try this. If it does not work let me know. – Amit Hasan Jul 22 '20 at 16:47
1

AFAIK the only way ATM is to use Select on the query:

List<Note> a = dBContext.Notes
    .Where(x => x.AuthorID == myId)
    .Select(x => new Note
    {
        ... select all note props,
        Images = x.Images.Select(i => new Image
        {
            ID = i.ID,
            Thumbnail = i.Thumbnail
        }
        .ToList()
    })
    .ToList();

UPD

Missed that you are using EF6. Then, I'm afraid you will need not only use Select, but custom DTOs/anonymous classes, for example:

var a = dBContext.Notes
    .Where(x => x.AuthorID == myId)
    .Select(x => new 
    {
        ... select all note props,
        Images = x.Images.Select(i => new 
        {
            ID = i.ID,
            Thumbnail = i.Thumbnail
        }
        .ToList()
    })
    .ToList();
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • I tested the Select query like you suggest. When doing so I recieve a NotSupportedException: "The entity of complex type Note cannot be constructed in a LINQ to Entities query". – denver Jul 22 '20 at 15:20
  • @denver yep, forgot about this limitation of EF6, then DTOs or anonymous types [should help](https://stackoverflow.com/a/5325861/2501279). – Guru Stron Jul 22 '20 at 15:25
  • When you say limitation of EF6 are you saying the above would work on something newer like EF Core? – denver Jul 22 '20 at 15:50