@@ -316,10 +316,20 @@ public ISelect<T1> Include<TNavigate>(Expression<Func<T1, TNavigate>> navigateSe
316316
317317 static MethodInfo GetEntityValueWithPropertyNameMethod = typeof ( EntityUtilExtensions ) . GetMethod ( "GetEntityValueWithPropertyName" ) ;
318318 static ConcurrentDictionary < Type , ConcurrentDictionary < string , MethodInfo > > _dicTypeMethod = new ConcurrentDictionary < Type , ConcurrentDictionary < string , MethodInfo > > ( ) ;
319- public ISelect < T1 > IncludeMany < TNavigate > ( Expression < Func < T1 , ICollection < TNavigate > > > navigateSelector , Action < ISelect < TNavigate > > then = null ) where TNavigate : class {
319+ public ISelect < T1 > IncludeMany < TNavigate > ( Expression < Func < T1 , IEnumerable < TNavigate > > > navigateSelector , Action < ISelect < TNavigate > > then = null ) where TNavigate : class {
320+ var throwNavigateSelector = new Exception ( "IncludeMany 参数1 类型错误,表达式类型应该为 MemberAccess" ) ;
321+
320322 var expBody = navigateSelector ? . Body ;
321323 if ( expBody == null ) return this ;
322- if ( expBody . NodeType != ExpressionType . MemberAccess ) throw new Exception ( "IncludeMany 参数1 类型错误,表达式类型应该为 MemberAccess" ) ;
324+ MethodCallExpression whereExp = null ;
325+ if ( expBody . NodeType == ExpressionType . Call ) {
326+ throwNavigateSelector = new Exception ( $ "IncludeMany { nameof ( navigateSelector ) } 参数类型错误,表达式格式应该是 a.collection.Where(c => c.aid == a.id)") ;
327+ whereExp = ( expBody as MethodCallExpression ) ;
328+ if ( whereExp . Method . Name != "Where" ) throw throwNavigateSelector ;
329+ expBody = whereExp . Object ?? whereExp . Arguments . FirstOrDefault ( ) ;
330+ }
331+
332+ if ( expBody . NodeType != ExpressionType . MemberAccess ) throw throwNavigateSelector ;
323333 var collMem = expBody as MemberExpression ;
324334 Expression tmpExp = collMem . Expression ;
325335 var members = new Stack < MemberInfo > ( ) ;
@@ -335,11 +345,67 @@ public ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, ICollection<TNavig
335345 isbreak = true ;
336346 break ;
337347 default :
338- throw new Exception ( "IncludeMany 参数1 类型错误" ) ;
348+ throw throwNavigateSelector ;
339349 }
340350 }
341351 var tb = _commonUtils . GetTableByEntity ( collMem . Expression . Type ) ;
342- if ( tb == null ) throw new Exception ( "IncludeMany 参数1 类型错误" ) ;
352+ if ( tb == null ) throw throwNavigateSelector ;
353+ var tbNav = _commonUtils . GetTableByEntity ( typeof ( TNavigate ) ) ;
354+ if ( tbNav == null ) throw new Exception ( $ "类型 { typeof ( TNavigate ) . FullName } 错误,不能使用 IncludeMany") ;
355+ TableRef tbref = null ;
356+
357+ if ( whereExp == null ) {
358+
359+ tbref = tb . GetTableRef ( collMem . Member . Name , true ) ;
360+
361+ } else {
362+ //处理临时关系映射
363+ tbref = new TableRef {
364+ RefType = TableRefType . OneToMany ,
365+ Property = tb . Properties [ collMem . Member . Name ] ,
366+ RefEntityType = tbNav . Type
367+ } ;
368+ foreach ( var whereExpArg in whereExp . Arguments ) {
369+ if ( whereExpArg . NodeType != ExpressionType . Lambda ) continue ;
370+ var whereExpArgLamb = whereExpArg as LambdaExpression ;
371+
372+ Action < Expression > actWeiParse = null ;
373+ actWeiParse = expOrg => {
374+ var binaryExp = expOrg as BinaryExpression ;
375+ if ( binaryExp == null ) throw throwNavigateSelector ;
376+
377+ switch ( binaryExp . NodeType ) {
378+ case ExpressionType . AndAlso :
379+ actWeiParse ( binaryExp . Left ) ;
380+ actWeiParse ( binaryExp . Right ) ;
381+ break ;
382+ case ExpressionType . Equal :
383+ var leftP1MemberExp = binaryExp . Left as MemberExpression ;
384+ var rightP1MemberExp = binaryExp . Right as MemberExpression ;
385+ if ( leftP1MemberExp == null || rightP1MemberExp == null ) throw throwNavigateSelector ;
386+ if ( leftP1MemberExp . Expression != tmpExp && leftP1MemberExp . Expression != whereExpArgLamb . Parameters [ 0 ] ||
387+ rightP1MemberExp . Expression != tmpExp && rightP1MemberExp . Expression != whereExpArgLamb . Parameters [ 0 ] ) throw throwNavigateSelector ;
388+
389+ if ( leftP1MemberExp . Expression == tmpExp && rightP1MemberExp . Expression == whereExpArgLamb . Parameters [ 0 ] ) {
390+ tbref . Columns . Add ( tb . ColumnsByCs [ leftP1MemberExp . Member . Name ] ) ;
391+ tbref . RefColumns . Add ( tbNav . ColumnsByCs [ rightP1MemberExp . Member . Name ] ) ;
392+ return ;
393+ }
394+ if ( rightP1MemberExp . Expression == tmpExp && leftP1MemberExp . Expression == whereExpArgLamb . Parameters [ 0 ] ) {
395+ tbref . Columns . Add ( tb . ColumnsByCs [ rightP1MemberExp . Member . Name ] ) ;
396+ tbref . RefColumns . Add ( tbNav . ColumnsByCs [ leftP1MemberExp . Member . Name ] ) ;
397+ return ;
398+ }
399+
400+ throw throwNavigateSelector ;
401+ default : throw throwNavigateSelector ;
402+ }
403+ } ;
404+ actWeiParse ( whereExpArgLamb . Body ) ;
405+ break ;
406+ }
407+ if ( tbref . Columns . Any ( ) == false ) throw throwNavigateSelector ;
408+ }
343409
344410 if ( collMem . Expression . NodeType != ExpressionType . Parameter )
345411 _commonExpression . ExpressionWhereLambda ( _tables , Expression . MakeMemberAccess ( collMem . Expression , tb . Properties [ tb . ColumnsByCs . First ( ) . Value . CsName ] ) , null ) ;
@@ -349,7 +415,6 @@ public ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, ICollection<TNavig
349415 if ( list == null ) return ;
350416 if ( list . Any ( ) == false ) return ;
351417
352- var tbref = tb . GetTableRef ( collMem . Member . Name , true ) ;
353418 if ( tbref . Columns . Any ( ) == false ) return ;
354419
355420 var t1parm = Expression . Parameter ( typeof ( T1 ) ) ;
0 commit comments