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

时间: 2024-08-05 05:01:29

扩展方法解决LinqToSql Contains超过2100行报错问题的相关文章

导入Maven 工程pom.xml首行报错解决方法

1.利用IDE导入一个Maven工程,但是pom.xml文件首行报错,发现是maven版本需要升级 2.在pom.xml文件 增加配置 <properties> <maven-jar-plugin.version>2.6</maven-jar-plugin.version></properties> 3.以eclipse为例,菜单help->install new software Name:MavenArchiver location:http://

springboot x.x.x RELEASE pom 第一行报错解决办法

springboot x.x.x RELEASE pom 第一行报错解决办法 在pom.xml 文件的properties中加入maven jar插件的版本号 <properties> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties> 再在项目右键maven ,update project 更新项目即可 原文地址:https://www.cnblogs.com/Y

创建maven项目pom.xml第一行报错

之前也创建过几次maven项目,也是第一行报错,之前直接是右键项目强制更新maven好像就解决了,这次遇见这个问题使用这个方法好像不起作用了,给的一堆英文报错又看不懂,幸好在网上看见路人甲大神提示,根据报错信息(虽然大部分看不懂)找到给出的jar文件和路径,然后根据路径找到提示的jar所在文件夹,就会发现这个文件夹没有jar包,最后只需要在网上下载这个jar放进去就好了,不知道为什么下好的maven竟然还会jar缺失.......

解决Azure Stack App Service部署报错一例

解决Azure Stack App Service部署报错一例 Azure Stack是微软新一代的云基础架构,是Windows Server 2016.Azure Pack和Azure Service Fabric的组合,能够将Azure的IaaS和PaaS功能带到客户自己的数据中心里,微软希望所有用户都能体验到该项服务,近期Azure Stack发布了GA版本,小弟有幸对其进行部署测,和之前版本相比GA版本提供了GUI的部署界面,同时在稳定性上也大大有所提升!但小弟在部署App Servic

maven项目pom.xml第一行报错

maven项目pom.xml第一行报错 这是第一行:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&q

解决window7 x64位Anaconda启动报错:AttributeError: &#39;_NamespacePath&#39; object has no attribute &#39;sort&#39;

最近论文需要用到python做数据分析,python语法简单,但是Windows下安装第三方包恶心的要命,statsmodels用pip死活安装不上,网上查了说包相互依赖windows下的pip不能下载全,还有好几个其他的统计包也是如此,整晕了算. 看网上有些python大牛推荐Anaconda,可以解决包的问题,于是卸载本地的python,从官网上下了个Anconda玩玩,结果遇到新问题. 问题如下: An unexpected error has occurred. Please consi

pom.xm首行报错Failure to transfer org.codehaus.plexus:plexus-components:pom:1.1.20

从SVN导出一个Maven项目,pom.xml首行报错: Failure to transfer org.codehaus.plexus:plexus-components:pom:1.1.20 from http://maven.oschina.net/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of

烂泥:【解决】ubuntu使用远程NFS报错

本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天在ubuntu系统上使用远程NFS,发现一直报错无法使用. 查看NFS挂载命令没有错误,命令如下: mount -t nfs 192.168.1.11:/data/work/soft/iso /home/lianzhou/iso 查看相关资料说,因为ubunutu14默认是没有安装NFS客户端的,需要安装NFS客户端. 安装NFS客户端,使用如下命令: sudo apt-get –y install nfs-common 安装完毕后,再次使用m

pom文件第一行报错(unknown)

问题: Eclipse导入maven项目时,或者新建一个springboot项目时,pom.xml文件第一行报错,没有错误信息提示,就一个Unknown,但是项目可以正常运行. 如下图: 原因: 因为版本升级了(2.2.6.RELEASE),开发工具不兼容导致. 解决: 在pom.xml 文件中的 properties 加入maven jar 插件的版本号.如下图: 然后maven --> update project 就可以发现万恶的小红叉消失了. 注意:这里的3.1.1版本不是自己用的mav