从包含中排除 属性
Exclude Property From Include
我的模型如下所示:
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; }
}
我可以加载以下内容:
List<Note> a = dBContext.Notes.Where(x => x.AuthorID == myId)
.Include(x => x.Images).ToList();
但是我想避免加载 Image 的 FullRes 成员,因为它很大。以下内容(抛出 ArgumentException - 包含路径表达式必须引用在类型上定义的导航 属性。使用虚线路径作为参考导航属性,使用 Select 运算符进行集合导航属性。):
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();
有人知道实现这个目标的正确语法吗?
我没有测试。你可以尝试这样的事情。
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();
据我所知,ATM 的唯一方法是在查询中使用 Select
:
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
没有注意到您正在使用 EF6。那么,恐怕你不仅需要使用 Select
,还需要自定义 DTOs/anonymous 类,例如:
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();
我的模型如下所示:
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; }
}
我可以加载以下内容:
List<Note> a = dBContext.Notes.Where(x => x.AuthorID == myId)
.Include(x => x.Images).ToList();
但是我想避免加载 Image 的 FullRes 成员,因为它很大。以下内容(抛出 ArgumentException - 包含路径表达式必须引用在类型上定义的导航 属性。使用虚线路径作为参考导航属性,使用 Select 运算符进行集合导航属性。):
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();
有人知道实现这个目标的正确语法吗?
我没有测试。你可以尝试这样的事情。
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();
据我所知,ATM 的唯一方法是在查询中使用 Select
:
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
没有注意到您正在使用 EF6。那么,恐怕你不仅需要使用 Select
,还需要自定义 DTOs/anonymous 类,例如:
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();