c# - 通用表达式过滤器

标签 c# linq generics expression

您好,我希望有人可以提供以下方面的一些指导:

我正在尝试创建一个使用反射的通用表达式过滤器,它可以接收一组规则并创建一个有效的表达式来限制从数据源返回的数据。

我遇到的问题是试图了解如何编写表达式来处理以下情况。

public class Item
{
    public string Name { get; set; }
    public List<TagTypes> TagTypes { get; set; }
}

public class TagTypes
{
    public string Name { get; set; }
    public List<Tags> Tags { get; set; }
}

public class Tags
{
    public string TagName { get; set; }
}

数据基本上看起来像

var itemList = new List<Item>
            {
                new Item
                {
                Name = "xyz",
                TagTypes = new List<TagTypes>
                    {
                        new TagTypes
                        {
                            Name = "Genre",
                            Tags = new List<Tags>
                                {
                                new Tags
                                    {
                                        TagName = "tag1"
                                    },
                                new Tags
                                    {
                                        TagName = "tag2"
                                    }        
                                }
                        }
                    }
               }

            };

我想编写一个表达式,返回所有匹配“流派”过滤器的项目 --> “tag1”

我可以使用以下方法做到这一点:

var filtered = from item in itemList
from tagType in item.TagTypes
   where tagType.Name == "Genre"
      from tag in tagType.Tags
      where tag.TagName == "tag1"
select item

并且有一个基本的工作版本,可以很好地处理 Item 类的属性,但是我不知道如何在 TagTypes 和下面的 SelectMany 上执行。

Basic级别就是下面的

var parameter = Expression.Parameter(typeof(T), "itemList");
var property = GetPropertyInfo<T>({Propertyname});
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
Expression.Equal(propertyAccess, Expression.Constant({Value}));

编辑

数据存储在 NoSQL 数据库中,过滤器是动态的,可以更改和添加。

创建带反射的通用过滤器的原因是为了适应不同的过滤器,而不必事先知道它们是什么。用户将能够使用过滤器 ID 请求数据,然后这将根据请求中的过滤器选择数据。

最佳答案

我认为你可能把这个复杂化了。

你可以写一个Expression<Func<Item>>Expression<Func<List<Item>>并选择 Body属性(property)。

例如

 Expression<Func<List<Item>> createListOfItems = () => new List<Item>
        {
            new Item
            {
            Name = "xyz",
            TagTypes = new List<TagTypes>
                {
                    new TagTypes
                    {
                        Name = "Genre",
                        Tags = new List<Tags>
                            {
                            new Tags
                                {
                                    TagName = "tag1"
                                },
                            new Tags
                                {
                                    TagName = "tag2"
                                }        
                            }
                    }
                }
           }

        };

关于c# - 通用表达式过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10943981/

相关文章:

c# - 停止 .NET 线程最不优雅的方法是什么?

c# - 如何使用 LINQ-to-XML 将以下 XML 加载到字典中?

c# - 遍历两个相同的数据源

java - 创建通用重映射函数

c# - 当我将可空 int 转换为字符串时,为什么 null 合并运算符不对它起作用?

c# - Windows窗体打开其他窗体

c# - 短路 linq 查询抛出 null 错误

java - 如何为 JAVA 中的泛型类型提供默认值

c# - 在 new Thread() 中创建控件时在正确的线程上调用方法