【发布时间】:2022-01-02 15:44:45
【问题描述】:
给定以下模型
public class ApiImageModel
{
public int ID { get; set; }
...
public List<TagModel> Tags { get; set; } = new();
}
和
public class TagModel
{
public int ID { get; set; }
...
public string Name { get; set; }
public List<ApiImageModel> Images { get; set; } = new();
}
如何使用 Linq 根据给定的一组 TagModel 查询 ApiImageModel 列表? 我现在为此苦苦挣扎了一段时间,我当然缺少一些基本的东西,但我无法确定它。
我为 EF6 尝试了这种方法: EF6 How to query where children contains all values of a list
像这样,将所有 TagModel-ID 保存在一个数组“tagIDs”中:
int[] tagIDs;
...
IQueryable<ApiImageModel> images = context.Images.Where(image => tagIDs.All(id => image.Tags.Any(tag => tag.ID == id)));
但 Visual Studio 用“InvalidOperationException”奖励我:
The LINQ expression 'DbSet<ApiImageModel>()
.Where(a => __tagIDs_0
.All(id => DbSet<Dictionary<string, object>>("ApiImageModelTagModel")
.Where(a0 => EF.Property<Nullable<int>>(a, "ID") != null && object.Equals(
objA: (object)EF.Property<Nullable<int>>(a, "ID"),
objB: (object)EF.Property<Nullable<int>>(a0, "ImagesID")))
.Join(
inner: DbSet<TagModel>(),
outerKeySelector: a0 => EF.Property<Nullable<int>>(a0, "TagsID"),
innerKeySelector: t => EF.Property<Nullable<int>>(t, "ID"),
resultSelector: (a0, t) => new TransparentIdentifier<Dictionary<string, object>, TagModel>(
Outer = a0,
Inner = t
))
.Any(ti => ti.Inner.ID == id)))' could not be translated.
我很高兴得到一些帮助:)
【问题讨论】:
标签: c# sqlite entity-framework-core many-to-many linq-to-entities