这是什么错误:
The parameter ‘d’ was not bound in the specified LINQ to Entities
query expression
查看详细信息 :
private static IEnumerable<T> ConnectToDatabase(IQueryable dbSet,ParameterExpression pe,IEnumerable<Expression> expressions,string orderby,bool desc) { // expressions = Expression body = null; if (expressions.Any()) { foreach (Expression expression in expressions) { body = ExpressionExtensions.JoinExpressions(body == null,Expression.Or,body,expression); } } if (body == null) { Expression left = Expression.Property(pe,"ID"); Expression right = Expression.Constant(-1); body = Expression.NotEqual(left,right); } IQueryable<T> results; MethodCallExpression whereCallExpression = Expression.Call( typeof(Queryable),"Where",new[] { dbSet.ElementType },dbSet.Expression,Expression.Lambda<Func<T,bool>>(body,new[] { pe })); var ModelType = typeof(T); pe = Expression.Parameter(ModelType,"x"); var propertyInfoOrderBy = GetPropertyInfo(orderby); var propertyAccess = Expression.MakeMemberAccess(pe,propertyInfoOrderBy); var orderByExp = Expression.Lambda(propertyAccess,pe); MethodCallExpression orderByCallExpression = Expression.Call( typeof(Queryable),"OrderBy",new[] { ModelType,propertyInfoOrderBy.PropertyType },whereCallExpression,Expression.Quote(orderByExp)); if (desc) { MethodCallExpression resultExp = Expression.Call( typeof(Queryable),"OrderByDescending",orderByCallExpression,Expression.Quote(orderByExp)); results = dbSet.Provider.createquery<T>(resultExp); } else { results = dbSet.Provider.createquery<T>(orderByCallExpression); } return results.ToList(); }
表达式:
身体 :
whereCallExpression:
orderByCallExpression:
错误:
JN_News课程:
public class JN_News { public int ID { get; set; } public string Title { get; set; } public string Description { get; set; } public string NewsLink { get; set; } public DateTime PubDate { get; set; } public string ImageLink { get; set; } public bool Isdisplay { get; set; } public Decimal? Rate { get; set; } public int Newscategories_ID { get; set; } public virtual JN_Newscategories JN_Newscategories { get; set; } }
JN_Newscategories类:
public class JN_Newscategories { public int ID { get; set; } public string NewscategoriesFa { get; set; } public string NewscategoriesEn { get; set; } public bool IsGetNews { get; set; } public virtual ICollection<JN_News> JN_News { get; set; } public JN_Newscategories() { JN_News = new Collection<JN_News>(); } }
更新:
当我消除了以下两个陈述时.好好工作:
我的所有代码:
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace NewsSiteApk.Data.DynamicSearchLibrary { public static class SearchUsingExpression<T> where T : class { public static IEnumerable<T> Search(IEnumerable<T> listofT,string search,string orderBy,bool desc,int pageIndex,int pageSize) { listofT = GetlistofData(listofT,search,orderBy,desc).Skip((pageIndex - 1) * pageSize).Take(pageSize); return listofT; } public static int GetCount(IEnumerable<T> listofT,string search) { listofT = GetlistofData(listofT,"id",true); return listofT.Count(); } private static IEnumerable<T> GetlistofData(IEnumerable<T> listofT,bool desc) { var modelType = typeof(T); ParameterExpression pe = Expression.Parameter(modelType,"d"); var expressions = new List<Expression>(); if (!string.IsNullOrEmpty(search)) { expressions.AddRange(GetExpressions(modelType.Name,pe)); } var connectToDatabase = ConnectToDatabase(listofT.AsQueryable(),pe,expressions,desc); return connectToDatabase; } private static IEnumerable<T> ConnectToDatabase(IQueryable dbSet,bool desc) { Expression body = null; if (expressions.Any()) { foreach (Expression expression in expressions) { body = ExpressionExtensions.JoinExpressions(body == null,expression); } } if (body == null) { Expression left = Expression.Property(pe,"ID"); Expression right = Expression.Constant(-1); body = Expression.NotEqual(left,right); } IQueryable<T> results; MethodCallExpression whereCallExpression = Expression.Call( typeof(Queryable),new[] { pe })); var propertyInfoOrderBy = GetPropertyInfo(orderby); var propertyAccess = Expression.MakeMemberAccess(pe,propertyInfoOrderBy); var orderByExp = Expression.Lambda(propertyAccess,pe); var ModelType = typeof(T); MethodCallExpression orderByCallExpression = Expression.Call( typeof(Queryable),Expression.Quote(orderByExp)); if (desc) { MethodCallExpression resultExp = Expression.Call( typeof(Queryable),Expression.Quote(orderByExp)); results = dbSet.Provider.createquery<T>(resultExp); } else { results = dbSet.Provider.createquery<T>(orderByCallExpression); } return results.ToList(); } private static IEnumerable<Expression> GetExpressions(string modelName,ParameterExpression pe) { var expressions = new List<Expression>(); var fieldModels = GetFields(modelName); foreach (FieldModel fieldModel in fieldModels) { IEnumerable<Expression> conditionsWithSubModel; if (fieldModel.NameEn.Contains("[]")) { conditionsWithSubModel = GetConditionsWithSubModelList(search,fieldModel.NameEn,pe); } else if (fieldModel.NameEn.Contains(".")) { conditionsWithSubModel = GetConditionsWithSubModel(search,fieldModel.NameEn); } else { conditionsWithSubModel = GetConditionsWithoutSubModel(search,fieldModel.NameEn); } if (conditionsWithSubModel != null) expressions.AddRange(conditionsWithSubModel); } return expressions; } private static IEnumerable<Expression> GetConditionsWithoutSubModel(string search,string parametr) { var expressions = new List<Expression>(); foreach (var splitSeacrh in search.Split(' ')) { Expression left = Expression.Property(pe,parametr); Expression right = Expression.Constant(splitSeacrh); MethodCallExpression conditionExpression = Expression.Call(left,typeof(string).getmethod("Contains"),right); expressions.Add(conditionExpression); } return expressions; } private static IEnumerable<Expression> GetConditionsWithSubModel(string search,string parameter) { // output.Where(d => d.JN_Newscategories.NewscategoriesEn.Contains("")); var expressions = new List<Expression>(); var strings = parameter.Split('$'); var modelName = strings[0]; // Like : JN_News var subModelName = strings[1].Split('.')[0];// Like : JN_Newscategories var subModelField = strings[1].Split('.')[1];// Like : NewscategoriesEn foreach (var splitSeacrh in search.Split(' ')) { Type modelClass = GetModel(modelName); Type submodelClass = GetModel(subModelName); ParameterExpression peSubModel = Expression.Parameter(modelClass,"d"); Expression leftSubModel = Expression.Property(peSubModel,modelClass.GetProperty(subModelName)); Expression ex = Expression.Property(leftSubModel,submodelClass.GetProperty(subModelField)); Expression rightSubModel = Expression.Constant(splitSeacrh); MethodCallExpression conditionExpressionSubModel = Expression.Call(ex,rightSubModel); expressions.Add(conditionExpressionSubModel); } return expressions; } private static IEnumerable<Expression> GetConditionsWithSubModelList(string search,string parameter,ParameterExpression peModel) { parameter = parameter.Replace("[]",string.Empty); var expressions = new List<Expression>(); var subModelName = parameter.Split('.')[0]; var subModelField = parameter.Split('.')[1]; foreach (var splitSeacrh in search.Split(' ')) { Type modelClass = GetModel(subModelName); var subModelProperty = GetPropertyInfo(subModelName); ParameterExpression peSubModel = Expression.Parameter(modelClass,modelClass.GetProperty(subModelField)); Expression rightSubModel = Expression.Constant(splitSeacrh); MethodCallExpression conditionExpressionSubModel = Expression.Call(leftSubModel,rightSubModel); LambdaExpression anyLambdaSubModelForModel = Expression.Lambda(conditionExpressionSubModel,peSubModel); MethodInfo anyMethodForModel = CreateAnyMethodGeneric(subModelProperty); Expression lambedaSubModelForExpressionModel = Expression.Property(peModel,subModelProperty); Expression expression = Expression.Call(anyMethodForModel,lambedaSubModelForExpressionModel,anyLambdaSubModelForModel); expressions.Add(expression); } return expressions; } private static Type GetModel(string name) { return (typeof(T).Assembly).GetTypes() .First(d => string.Equals(d.Name,name,StringComparison.CurrentCultureIgnoreCase)); } private static PropertyInfo GetPropertyInfo(string name) { return typeof(T).GetProperties().First(d => string.Equals(d.Name,StringComparison.CurrentCultureIgnoreCase)); } private static MethodInfo CreateAnyMethodGeneric(PropertyInfo propYekiBeAkhar,string methodName = "Any") { return typeof(Enumerable).getmethods() .Single(m => m.Name == methodName && m.GetParameters().Length == 2) .MakeGenericmethod(propYekiBeAkhar.PropertyType.GenericTypeArguments[0]); } private static IEnumerable<FieldModel> GetFields(string modelName) { var fieldsFactory = new FieldsFactory(); var fieldModels = fieldsFactory.GetClause(modelName); return fieldModels; } } }
野外工厂类:
public class FieldsFactory { public List<FieldModel> GetClause(string modelName) { var type = typeof(FieldsFactory); var methodInfos = type.getmethod("Get" + modelName + "Fields",BindingFlags.NonPublic | BindingFlags.Instance); var listofFields = (List<FieldModel>)methodInfos.Invoke(this,null); return listofFields; } private List<FieldModel> GetJN_NewscategoriesFields() { var fields = new List<FieldModel> { new FieldModel { NameEn = "NewscategoriesFa",},new FieldModel { NameEn = "NewscategoriesEn",new FieldModel { NameEn = "JN_News[].Title",new FieldModel { NameEn = "JN_News[].Description",} }; return fields; } private List<FieldModel> GetJN_NewsFields() { var fields = new List<FieldModel> { new FieldModel { NameEn = "Title",new FieldModel { NameEn = "JN_News$JN_Newscategories.NewscategoriesFa",new FieldModel { NameEn = "JN_News$JN_Newscategories.NewscategoriesEn",} }; return fields; } }
解决方法
我fooooooooooound.我很开心.
只是,我替换了以下方法:
private static IEnumerable<Expression> GetConditionsWithSubModel(string search,string parameter) { // output.Where(d => d.JN_Newscategories.NewscategoriesEn.Contains("")); var expressions = new List<Expression>(); var strings = parameter.Split('$'); var modelName = strings[0]; var subModelName = strings[1].Split('.')[0]; var subModelField = strings[1].Split('.')[1]; foreach (var splitSeacrh in search.Split(' ')) { Type modelClass = GetModel(modelName); Type submodelClass = GetModel(subModelName); ParameterExpression peSubModel = Expression.Parameter(modelClass,"d"); Expression leftSubModel = Expression.Property(peSubModel,modelClass.GetProperty(subModelName)); Expression ex = Expression.Property(leftSubModel,submodelClass.GetProperty(subModelField)); Expression rightSubModel = Expression.Constant(splitSeacrh); MethodCallExpression conditionExpressionSubModel = Expression.Call(ex,rightSubModel); expressions.Add(conditionExpressionSubModel); } return expressions; }
使用以下方法:
private static IEnumerable<Expression> GetConditionsWithSubModel(string search,string parameter) { // output.Where(d => d.JN_Newscategories.NewscategoriesEn.Contains("")); var expressions = new List<Expression>(); var strings = parameter.Split('$'); var modelName = strings[0]; var subModelName = strings[1].Split('.')[0]; var subModelField = strings[1].Split('.')[1]; foreach (var splitSeacrh in search.Split(' ')) { Type modelClass = GetModel(modelName); Type submodelClass = GetModel(subModelName); Expression leftSubModel = Expression.Property(pe,rightSubModel); expressions.Add(conditionExpressionSubModel); } return expressions; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。