程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 擴展方法解決LinqToSql Contains超過2100行報錯問題,linqtosql2100行

擴展方法解決LinqToSql Contains超過2100行報錯問題,linqtosql2100行

編輯:C#入門知識

擴展方法解決LinqToSql Contains超過2100行報錯問題,linqtosql2100行


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved