The extension method solves the problem of LinqToSql containing more than 2100 rows and reports an error in linqtosql2100 rows.

Source: Internet
Author: User

The extension method solves the problem of LinqToSql containing more than 2100 rows and reports an error in linqtosql2100 rows.
1. Extension Method

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. Call

Using Utils;

Void Test ()

{

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

Var list = datacontext. TestTable. InRange (ee = & gt; ee. Id, 2000, ids). ToList ();

}

Solution from: http://stackoverflow.com/questions/567963/linq-expression-to-return-property-value/568771#568771

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.