Linq 为什么谓词不是';在通过反射进行构建时不进行过滤
我正在基于SearchObject构建一个相当大的过滤器,它有50多个可以搜索的字段 我不想为每一个单独构建where子句,我想我应该使用一些简单的方法,尝试构建提供必要信息的自定义属性,然后使用反射来构建我的每个谓词语句(使用LinqKit btw)。问题是,代码在反射代码中找到了适当的值,并成功地为属性构建了一个谓词,但“where”似乎没有实际生成,并且我的查询总是返回0条记录 属性很简单:Linq 为什么谓词不是';在通过反射进行构建时不进行过滤,linq,reflection,custom-attributes,Linq,Reflection,Custom Attributes,我正在基于SearchObject构建一个相当大的过滤器,它有50多个可以搜索的字段 我不想为每一个单独构建where子句,我想我应该使用一些简单的方法,尝试构建提供必要信息的自定义属性,然后使用反射来构建我的每个谓词语句(使用LinqKit btw)。问题是,代码在反射代码中找到了适当的值,并成功地为属性构建了一个谓词,但“where”似乎没有实际生成,并且我的查询总是返回0条记录 属性很简单: [AttributeUsage(AttributeTargets.Property, AllowM
[AttributeUsage(AttributeTargets.Property, AllowMultiple=true)]
public class FilterAttribute: Attribute
{
public FilterType FilterType { get; set; } //enum{ Object, Database}
public string FilterPath { get; set; }
//var predicate = PredicateBuilder.False<Metadata>();
}
[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
公共类FilterAttribute:属性
{
公共筛选器类型筛选器类型{get;set;}//enum{Object,Database}
公共字符串筛选器路径{get;set;}
//var predicate=PredicateBuilder.False();
}
这是我构建查询的方法:
public List<ETracker.Objects.Item> Search(Search SearchObject, int Page, int PageSize)
{
var predicate = PredicateBuilder.False<ETracker.Objects.Item>();
Type t = typeof(Search);
IEnumerable<PropertyInfo> pi = t.GetProperties();
string title = string.Empty;
foreach (var property in pi)
{
if (Attribute.IsDefined(property, typeof(FilterAttribute)))
{
var attrs = property.GetCustomAttributes(typeof(FilterAttribute),true);
var value = property.GetValue(SearchObject, null);
if (property.Name == "Title")
title = (string)value;
predicate.Or(a => GetPropertyVal(a, ((FilterAttribute)attrs[0]).FilterPath) == value);
}
}
var res = dataContext.GetAllItems().Take(1000)
.Where(a => SearchObject.Subcategories.Select(b => b.ID).ToArray().Contains(a.SubCategory.ID))
.Where(predicate);
return res.ToList();
}
公共列表搜索(搜索SearchObject,int Page,int PageSize)
{
var predicate=PredicateBuilder.False();
类型t=类型(搜索);
IEnumerable pi=t.GetProperties();
string title=string.Empty;
foreach(pi中的var属性)
{
if(Attribute.IsDefined(property,typeof(filteratAttribute)))
{
var attrs=property.GetCustomAttributes(typeof(FilterAttribute),true);
var value=property.GetValue(SearchObject,null);
如果(property.Name==“Title”)
title=(字符串)值;
Or(a=>GetPropertyVal(a,((FilterAttribute)attrs[0]).FilterPath==value);
}
}
var res=dataContext.GetAllItems().Take(1000)
.Where(a=>SearchObject.Subcategories.Select(b=>b.ID).ToArray().Contains(a.SubCategory.ID))
.Where(谓语);
return res.ToList();
}
SearchObject非常简单:
public class Search
{
public List<Item> Items { get; set; }
[Filter(FilterType = FilterType.Object, FilterPath = "Title")]
public string Title { get; set; }
...
}
公共类搜索
{
公共列表项{get;set;}
[过滤器(FilterType=FilterType.Object,FilterPath=“Title”)]
公共字符串标题{get;set;}
...
}
如有任何建议,将不胜感激。我可能走错了方向,如果有人有更好的选择(或者至少有一个可行的选择),我不会生气。你没有在任何地方赋值谓词。将行更改为:
predicate = predicate.Or(a => GetPropertyVal(a, ((FilterAttribute)attrs[0]).FilterPath) == value);
只是一个想法。谓词。是否将相等性测试中的值作为常量或引用处理?如果作为引用,则Where正在查找特定引用,但未找到它们。