星空网 > 软件开发 > ASP.net

扩展方法解决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




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

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

进口货物物流:https://www.goluckyvip.com/tag/93273.html
外国物流:https://www.goluckyvip.com/tag/93274.html
国际物流物流:https://www.goluckyvip.com/tag/93275.html
跨境快递查询:https://www.goluckyvip.com/tag/93276.html
外贸的物流:https://www.goluckyvip.com/tag/93277.html
保税国际物流:https://www.goluckyvip.com/tag/93278.html
重庆品胜科技与星苹台达成合作 助力部队现代化后勤建设 :https://www.kjdsnews.com/a/1836523.html
回乡创业,不知道干什么,能否推荐几个农村老家创业的好项目?:https://www.vstour.cn/a/365177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流