Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 使用Linq搜索实体列表中是否存在所有属性_C#_Linq_Entity Framework_Predicatebuilder - Fatal编程技术网

C# 使用Linq搜索实体列表中是否存在所有属性

C# 使用Linq搜索实体列表中是否存在所有属性,c#,linq,entity-framework,predicatebuilder,C#,Linq,Entity Framework,Predicatebuilder,我拥有以下实体: [Table("Entities")] public abstract class Entity { public int EntityID { get; set; } public string Description { get; set; } public virtual ICollection<Tag> Tags { get; set; } } 如上所述,这种类型的标记是而不是重用的,只是存储为字符串。这使得确定实体是否共享标记变得稍微困难(而且

我拥有以下实体:

[Table("Entities")]
public abstract class Entity { 
 public int EntityID { get; set; } 
 public string Description { get; set; }
 public virtual ICollection<Tag> Tags { get; set; }
}
如上所述,这种类型的标记是而不是重用的,只是存储为字符串。这使得确定实体是否共享标记变得稍微困难(而且速度较慢)

我使用工作搜索返回实体列表,其中实体包含任何标记:

List<string> searchTags = new List<String> {"test1", "test2"};
entities = (_entityRepository.Entities
            .Where(o=>o.Tags.Any(f=>searchTags.Contains(f.TagValue)));
我想我已经到了需要更正DB模式以重用标记的时候了,当在标记列表上过滤我的实体时,这应该会提供一般的性能优势,但我仍然想问以下问题:

  • 我是否把这件事复杂化了,是否有一个简单的Linq语句来完成这件事,或者
  • 这是我应该使用谓词生成器来设置标准的东西吗
    可以这样做:

    var query = _entityRepository.Entities.Select(e => e);
    query = searchTags.Aggregate(query, (current, tag) => current.Where(e => e.Tags.Any(t => t.TagValue == tag)));
    var result = query.ToList();
    

    在完全测试之前,我必须对我的数据模型进行一些更改,但这似乎完成了我在退出测试更改时提出的要求(我不能花很多时间)。谢谢-帮助我找到正确的东西来完成其他一些任务。很高兴它有帮助。顺便说一句,回答问题2(一点):你知道System.Linq.Dynamic存在吗?如果您想动态构建linq查询,那么很好用
    entities = (_entityRepository.Entities
                .Where(o=>o.Tags.All(f=>f.TagValue.Contains(searchTags)));
    
    var query = _entityRepository.Entities.Select(e => e);
    query = searchTags.Aggregate(query, (current, tag) => current.Where(e => e.Tags.Any(t => t.TagValue == tag)));
    var result = query.ToList();