• 扩展方法解决LinqToSql Contains超过2100行报错问题


    1.扩展方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Linq.Expressions;
    using System.Reflection;
    
    namespace Utils
    {
        //http://stackoverflow.com/questions/567963/linq-expression-to-return-property-value/568771#568771
        public static class ContainsExtensions
        {
            public static IEnumerable<T> InRange<T, TValue>(
                    this IQueryable<T> source,
                    Expression<Func<T, TValue>> selector,
                    int blockSize,
                    IEnumerable<TValue> values)
            {
                MethodInfo method = null;
                foreach (MethodInfo tmp in typeof(Enumerable).GetMethods(
                        BindingFlags.Public | BindingFlags.Static))
                {
                    if (tmp.Name == "Contains" && tmp.IsGenericMethodDefinition
                            && tmp.GetParameters().Length == 2)
                    {
                        method = tmp.MakeGenericMethod(typeof(TValue));
                        break;
                    }
                }
                if (method == null) throw new InvalidOperationException(
                       "Unable to locate Contains");
                foreach (TValue[] block in values.GetBlocks(blockSize))
                {
                    var row = Expression.Parameter(typeof(T), "row");
                    var member = Expression.Invoke(selector, row);
                    var keys = Expression.Constant(block, typeof(TValue[]));
                    var predicate = Expression.Call(method, keys, member);
                    var lambda = Expression.Lambda<Func<T, bool>>(
                          predicate, row);
                    foreach (T record in source.Where(lambda))
                    {
                        yield return record;
                    }
                }
            }
            public static IEnumerable<T[]> GetBlocks<T>(
                    this IEnumerable<T> source, int blockSize)
            {
                List<T> list = new List<T>(blockSize);
                foreach (T item in source)
                {
                    list.Add(item);
                    if (list.Count == blockSize)
                    {
                        yield return list.ToArray();
                        list.Clear();
                    }
                }
                if (list.Count > 0)
                {
                    yield return list.ToArray();
                }
            }
        }
    }

    2.调用

    Using Utils;

    void Test()

    {

        var ids=new int[] { 1,2,3 ... 9999 };

        var list=datacontext.TestTable.InRange(ee => ee.Id, 2000, ids).ToList(); 

    }

    解决方案来自:http://stackoverflow.com/questions/567963/linq-expression-to-return-property-value/568771#568771

    From:http://www.cnblogs.com/xuejianxiyang/p/5491750.html

  • 相关阅读:
    Redis学习——数据结构下
    CI 笔记7,easyui 异步加载
    CI 笔记,借鉴的4个辅助自定义函数
    CI笔记6 json 传值
    CI 笔记,使用 json的参考文档(废弃)
    CI 笔记5 (CI3.0 默认控制器,多目录)
    CI 笔记(easyui js命令)
    CI 笔记4 (easyui 手风琴)
    CI 笔记3 (easyui 的layout布局,最小化layout原型)
    CI 笔记3 (easyui 和 js 排错)
  • 原文地址:https://www.cnblogs.com/xuejianxiyang/p/5491750.html
Copyright © 2020-2023  润新知