mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 增加 List\<T1\> 扩展方法 IncludeMany,实现从已知的内存 List 数据,进行和 ISelect.IncludeMany 相同功能的贪婪加载;
This commit is contained in:
		@@ -1155,6 +1155,8 @@ namespace FreeSql.Tests.Sqlite
 | 
				
			|||||||
            g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows();
 | 
					            g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows();
 | 
				
			||||||
            g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows();
 | 
					            g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            new List<Song>(new[] { song1, song2, song3 }).IncludeMany(g.sqlite, a => a.Tags);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var songs1 = g.sqlite.Select<Song>()
 | 
					            var songs1 = g.sqlite.Select<Song>()
 | 
				
			||||||
                .IncludeMany(a => a.Tags)
 | 
					                .IncludeMany(a => a.Tags)
 | 
				
			||||||
                .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id)
 | 
					                .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,7 @@ using System.ComponentModel;
 | 
				
			|||||||
using System.Data;
 | 
					using System.Data;
 | 
				
			||||||
using System.Drawing;
 | 
					using System.Drawing;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Linq.Expressions;
 | 
				
			||||||
using System.Reflection;
 | 
					using System.Reflection;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public static partial class FreeSqlGlobalExtensions
 | 
					public static partial class FreeSqlGlobalExtensions
 | 
				
			||||||
@@ -167,4 +168,26 @@ public static partial class FreeSqlGlobalExtensions
 | 
				
			|||||||
    public static ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Select<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this IFreeSql freesql) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class =>
 | 
					    public static ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Select<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this IFreeSql freesql) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class where T10 : class =>
 | 
				
			||||||
        freesql.Select<T1>().From<T2, T3, T4, T5, T6, T7, T8, T9, T10>((s, b, c, d, e, f, g, h, i, j) => s);
 | 
					        freesql.Select<T1>().From<T2, T3, T4, T5, T6, T7, T8, T9, T10>((s, b, c, d, e, f, g, h, i, j) => s);
 | 
				
			||||||
    #endregion
 | 
					    #endregion
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    #region IncludeMany
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// 本方法实现从已知的内存 List 数据,进行和 ISelect.IncludeMany 相同功能的贪婪加载<para></para>
 | 
				
			||||||
 | 
					    /// 示例:new List<Song>(new[] { song1, song2, song3 }).IncludeMany(g.sqlite, a => a.Tags);<para></para>
 | 
				
			||||||
 | 
					    /// 文档:https://github.com/2881099/FreeSql/wiki/%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd#%E5%AF%BC%E8%88%AA%E5%B1%9E%E6%80%A7-onetomanymanytomany
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    /// <typeparam name="T1"></typeparam>
 | 
				
			||||||
 | 
					    /// <typeparam name="TNavigate"></typeparam>
 | 
				
			||||||
 | 
					    /// <param name="list"></param>
 | 
				
			||||||
 | 
					    /// <param name="orm"></param>
 | 
				
			||||||
 | 
					    /// <param name="navigateSelector">选择一个集合的导航属性,也可通过 .Where 设置临时的关系映射,还可以 .Take(5) 每个子集合只取5条</param>
 | 
				
			||||||
 | 
					    /// <param name="then">即能 ThenInclude,还可以二次过滤(这个 EFCore 做不到?)</param>
 | 
				
			||||||
 | 
					    /// <returns></returns>
 | 
				
			||||||
 | 
					    public static List<T1> IncludeMany<T1, TNavigate>(this List<T1> list, IFreeSql orm, Expression<Func<T1, IEnumerable<TNavigate>>> navigateSelector, Action<ISelect<TNavigate>> then = null) where T1 : class where TNavigate : class
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (list == null || list.Any() == false) return list;
 | 
				
			||||||
 | 
					        var select = orm.Select<T1>().IncludeMany(navigateSelector, then) as FreeSql.Internal.CommonProvider.Select1Provider<T1>;
 | 
				
			||||||
 | 
					        select.SetList(list);
 | 
				
			||||||
 | 
					        return list;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    #endregion
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -348,7 +348,8 @@ namespace FreeSql
 | 
				
			|||||||
        /// <returns></returns>
 | 
					        /// <returns></returns>
 | 
				
			||||||
        ISelect<T1> Include<TNavigate>(Expression<Func<T1, TNavigate>> navigateSelector) where TNavigate : class;
 | 
					        ISelect<T1> Include<TNavigate>(Expression<Func<T1, TNavigate>> navigateSelector) where TNavigate : class;
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 贪婪加载集合的导航属性,其实是分两次查询,ToList 后进行了数据重装
 | 
					        /// 贪婪加载集合的导航属性,其实是分两次查询,ToList 后进行了数据重装<para></para>
 | 
				
			||||||
 | 
					        /// 文档:https://github.com/2881099/FreeSql/wiki/%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd#%E5%AF%BC%E8%88%AA%E5%B1%9E%E6%80%A7-onetomanymanytomany
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
        /// <typeparam name="TNavigate"></typeparam>
 | 
					        /// <typeparam name="TNavigate"></typeparam>
 | 
				
			||||||
        /// <param name="navigateSelector">选择一个集合的导航属性,也可通过 .Where 设置临时的关系映射,还可以 .Take(5) 每个子集合只取5条</param>
 | 
					        /// <param name="navigateSelector">选择一个集合的导航属性,也可通过 .Where 设置临时的关系映射,还可以 .Take(5) 每个子集合只取5条</param>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -956,5 +956,12 @@ namespace FreeSql.Internal.CommonProvider
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        internal void SetList(IEnumerable<T1> list)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            foreach (var include in _includeToList) include?.Invoke(list);
 | 
				
			||||||
 | 
					            _orm.Aop.ToList?.Invoke(this, new Aop.ToListEventArgs(list));
 | 
				
			||||||
 | 
					            _trackToList?.Invoke(list);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user