问题描述
代码运行后得出的SQL代码来不正确,company的被解析成Convert(value(WXMAPTEST.Form1+<>c__DisplayClass0).company))请高手帮忙,下面贴出所有代码privatevoidbutton1_Click(objectsender,EventArgse){using(SqlConnectionSQC=newSqlConnection()){SQC.ConnectionString="DataSource=TESTSERVER\TESTSERVER2005;InitialCatalog=WeiXin;IntegratedSecurity=False;UserID=sa;Password=kf10086;MultipleActiveResultSets=True";SQC.Open();DbQueryProviderDP=newDbQueryProvider(SQC);Query<WX_GoodsStock>Data=newQuery<WX_GoodsStock>(DP);intcompany=100;stringsss="rrrrr";IQueryable<WX_GoodsStock>query=Data.Where(u=>u.CompanyID==company&&u.GoodsName==sss);//输出SQL代码stringss=query.ToString();}}publicclassQuery<T>:IQueryable<T>,IQueryable,IEnumerable<T>,IEnumerable,IOrderedQueryable<T>,IOrderedQueryable{QueryProviderprovider;Expressionexpression;publicQuery(QueryProviderprovider){if(provider==null){thrownewArgumentNullException("provider");}this.provider=provider;this.expression=Expression.Constant(this);}publicQuery(QueryProviderprovider,Expressionexpression){if(provider==null){thrownewArgumentNullException("provider");}if(expression==null){thrownewArgumentNullException("expression");}if(!typeof(IQueryable<T>).IsAssignableFrom(expression.Type)){thrownewArgumentOutOfRangeException("expression");}this.provider=provider;this.expression=expression;}ExpressionIQueryable.Expression{get{returnthis.expression;}}TypeIQueryable.ElementType{get{returntypeof(T);}}IQueryProviderIQueryable.Provider{get{returnthis.provider;}}publicIEnumerator<T>GetEnumerator(){return((IEnumerable<T>)this.provider.Execute(this.expression)).GetEnumerator();}IEnumeratorIEnumerable.GetEnumerator(){return((IEnumerable)this.provider.Execute(this.expression)).GetEnumerator();}publicoverridestringToString(){returnthis.provider.GetQueryText(this.expression);}}publicabstractclassQueryProvider:IQueryProvider{protectedQueryProvider(){}IQueryable<S>IQueryProvider.CreateQuery<S>(Expressionexpression){returnnewQuery<S>(this,expression);}IQueryableIQueryProvider.CreateQuery(Expressionexpression){TypeelementType=TypeSystem.GetElementType(expression.Type);try{return(IQueryable)Activator.CreateInstance(typeof(Query<>).MakeGenericType(elementType),newobject[]{this,expression});}catch(TargetInvocationExceptiontie){throwtie.InnerException;}}SIQueryProvider.Execute<S>(Expressionexpression){return(S)this.Execute(expression);}objectIQueryProvider.Execute(Expressionexpression){returnthis.Execute(expression);}publicabstractstringGetQueryText(Expressionexpression);publicabstractobjectExecute(Expressionexpression);}internalstaticclassTypeSystem{internalstaticTypeGetElementType(TypeseqType){Typeienum=FindIEnumerable(seqType);if(ienum==null)returnseqType;returnienum.GetGenericArguments()[0];}privatestaticTypeFindIEnumerable(TypeseqType){if(seqType==null||seqType==typeof(string))returnnull;if(seqType.IsArray)returntypeof(IEnumerable<>).MakeGenericType(seqType.GetElementType());if(seqType.IsGenericType){foreach(TypearginseqType.GetGenericArguments()){Typeienum=typeof(IEnumerable<>).MakeGenericType(arg);if(ienum.IsAssignableFrom(seqType)){returnienum;}}}Type[]ifaces=seqType.GetInterfaces();if(ifaces!=null&&ifaces.Length>0){foreach(Typeifaceinifaces){Typeienum=FindIEnumerable(iface);if(ienum!=null)returnienum;}}if(seqType.BaseType!=null&&seqType.BaseType!=typeof(object)){returnFindIEnumerable(seqType.BaseType);}returnnull;}}internalclassObjectReader<T>:IEnumerable<T>,IEnumerablewhereT:class,new(){Enumeratorenumerator;internalObjectReader(DbDataReaderreader){this.enumerator=newEnumerator(reader);}publicIEnumerator<T>GetEnumerator(){Enumeratore=this.enumerator;if(e==null){thrownewInvalidOperationException("Cannotenumeratemorethanonce");}this.enumerator=null;returne;}IEnumeratorIEnumerable.GetEnumerator(){returnthis.GetEnumerator();}classEnumerator:IEnumerator<T>,IEnumerator,IDisposable{DbDataReaderreader;FieldInfo[]fields;int[]fieldLookup;Tcurrent;internalEnumerator(DbDataReaderreader){this.reader=reader;this.fields=typeof(T).GetFields();}publicTCurrent{get{returnthis.current;}}objectIEnumerator.Current{get{returnthis.current;}}publicboolMoveNext(){if(this.reader.Read()){if(this.fieldLookup==null){this.InitFieldLookup();}Tinstance=newT();for(inti=0,n=this.fields.Length;i<n;i++){intindex=this.fieldLookup[i];if(index>=0){FieldInfofi=this.fields[i];if(this.reader.IsDBNull(index)){fi.SetValue(instance,null);}else{fi.SetValue(instance,this.reader.GetValue(index));}}}this.current=instance;returntrue;}returnfalse;}publicvoidReset(){}publicvoidDispose(){this.reader.Dispose();}privatevoidInitFieldLookup(){Dictionary<string,int>map=newDictionary<string,int>(StringComparer.InvariantCultureIgnoreCase);for(inti=0,n=this.reader.FieldCount;i<n;i++){map.Add(this.reader.GetName(i),i);}this.fieldLookup=newint[this.fields.Length];for(inti=0,n=this.fields.Length;i<n;i++){intindex;if(map.TryGetValue(this.fields[i].Name,outindex)){this.fieldLookup[i]=index;}else{this.fieldLookup[i]=-1;}}}}}publicclassDbQueryProvider:QueryProvider{DbConnectionconnection;publicDbQueryProvider(DbConnectionconnection){this.connection=connection;}publicoverridestringGetQueryText(Expressionexpression){returnthis.Translate(expression);}publicoverrideobjectExecute(Expressionexpression){DbCommandcmd=this.connection.CreateCommand();cmd.CommandText=this.Translate(expression);DbDataReaderreader=cmd.ExecuteReader();TypeelementType=TypeSystem.GetElementType(expression.Type);returnActivator.CreateInstance(typeof(ObjectReader<>).MakeGenericType(elementType),BindingFlags.Instance|BindingFlags.NonPublic,null,newobject[]{reader},null);}privatestringTranslate(Expressionexpression){returnnewQueryTranslator().Translate(expression);}}