Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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
Entity framework core 查询实体框架核心中必须包含一组值的记录_Entity Framework Core - Fatal编程技术网

Entity framework core 查询实体框架核心中必须包含一组值的记录

Entity framework core 查询实体框架核心中必须包含一组值的记录,entity-framework-core,Entity Framework Core,我正在尝试在一个站点中创建一个过滤系统,该系统基于一组可以使用多个标记进行标记的记录。最终,我希望过滤器能够支持OR,AND,AND,NOT,但现在我只想开始工作 以下是具有相关属性的实体,这只是EF Core中建模的多对多关系 public class Record { public int Id { get; set; } public ICollection<RecordTag> RecordTags { get; set; } } public clas

我正在尝试在一个站点中创建一个过滤系统,该系统基于一组可以使用多个标记进行标记的记录。最终,我希望过滤器能够支持OR,AND,AND,NOT,但现在我只想开始工作

以下是具有相关属性的实体,这只是EF Core中建模的多对多关系

public class Record
{
     public int Id { get; set; }
     public ICollection<RecordTag> RecordTags { get; set; }
}

public class RecordTag
{
    public int RecordId { get; set; }
    public Song Record { get; set; }

    public int TagId { get; set; }
    public Tag Tag { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<RecordTag> RecordTags { get; set; }
}
公共类记录
{
公共int Id{get;set;}
公共ICollection记录标记{get;set;}
}
公共类记录标签
{
public int RecordId{get;set;}
公共歌曲记录{get;set;}
public int TagId{get;set;}
公共标记{get;set;}
}
公共类标签
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection记录标记{get;set;}
}
我尝试编写一个EF核心查询,如下所示:

var tagList = tags.Split(',');
// tagList now contains the strings of tags the user has filtered on

Records = recordRepository.Query() // simply returns IQueryable<Records> 
          .Where(r=> tagList.All(  // For each record, iterate through all tags selected by the user
                t => r.RecordTags.Select(rt => rt.Tag.Name).Contains(t))) // For each user selected tag, get all the tags on the record and ensure it contains the tag we're iterating over
                .Include(r => r.RecordTags).ThenInclude(rt => rt.Tag).ToList(); // Include the tag data back with the parent entity.
var tagList=tags.Split(',');
//标记列表现在包含用户已筛选的标记字符串
Records=recordRepository.Query()//只返回IQueryable
.Where(r=>tagList.All(//对于每个记录,遍历用户选择的所有标记
t=>r.RecordTags.Select(rt=>rt.Tag.Name).Contains(t))//对于每个用户选择的标记,获取记录上的所有标记,并确保它包含我们正在迭代的标记
.Include(r=>r.RecordTags)。然后Include(rt=>rt.Tag)。ToList();//将标记数据包含回父实体。
但是,这会抛出一个错误

无法翻译[查询]。以可以翻译的形式重写查询,或者通过插入对AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用显式切换到客户端计算

我不希望返回一个更大的集合并在应用服务器上对其进行过滤,而是直接针对数据库正确地构建查询


为什么这个查询无效?还有其他方法可以这样写吗?

您可以在foreach循环中添加where条件

var recordRepositoryQuery = recordRepository.Query();
foreach(var tag in taglist)
{       
   recordRepositoryQuery = recordRepositoryQuery.Where(r => r.RecordTags.Select(rt => rt.Tag.Name).Contains(tag))
}

非常感谢。这比试图在一个where子句中构建它更有意义,现在我可以看到,如果有人创建了一个包含and、OR和NOT的复杂表达式,我可以用这种方式动态构建查询。