最简单的方法:使用LINQKit(免费的非限制性许可证)
工作版本的代码:
public static IEnumerable<T> SelectByParameterList<T, PropertyType>(this Table<T> items, IEnumerable<PropertyType> parameterList, expression<Func<T, PropertyType>> propertySelector, int blockSize) where T : class{ var groups = parameterList .Select((Parameter, index) => new { GroupID = index / blockSize, //# of parameters per request Parameter } ) .GroupBy(x => x.GroupID) .AsEnumerable(); var selector = LinqKit.Linq.Expr(propertySelector); var results = groups .Select(g => new { Group = g, Parameters = g.Select(x => x.Parameter) } ) .SelectMany(g => items.AsExpandable().Where(item => g.Parameters.Contains(selector.Invoke(item))) ); return results;}
用法示例:
Guid[] local_refill_ids = Refills.Select(r => r.Id).Take(20).ToArray(); IEnumerable<Refill> results = Refills.SelectByParameterList(local_refill_ids, r => r.Id, 10); //runs 2 SQL queries with 10 parameters each
再次感谢你的帮助!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)