相关疑难解决方法(0)

从VS2010升级到VS2012后,Linq到EF加入"索引超出范围"

从Visual Studio 2010升级到2012后,代码开始抛出"ArgumentOutOfRangeException - 索引超出范围.必须是非负的且小于集合的大小.参数名称:索引"在使用联接的Linq查询上.

在LINQPad中使用以下简单示例(使用EF数据模型)给出了ArgumentOutOfRangeException:

void Main()
{
    var iq1 = Customers.Select(ap => ap.ID);
    var iq2 = iq1.Join(Customers.Select(ap => ap.ID),
                    a => a,
                    b => b,
                    (a, b) => new { a });

    iq2.Dump(); 
}
Run Code Online (Sandbox Code Playgroud)

更改前面的示例以返回包含连接两边的匿名对象不会给出ArgumentOutOfRangeException并按预期方式给出结果:

void Main()
{
    var iq1 = ActionPlans.Select(ap => ap.ID);
    var iq2 = iq1.Join(ActionPlans.Select(ap => ap.ID),
                    a => a,
                    b => b,
                    (a, b) => new { a, b });

    iq2.Dump(); 
}
Run Code Online (Sandbox Code Playgroud)

好的,所以由于某种原因我不得不返回连接的两边,但后来我尝试使用虚拟值代替以下示例,也执行没有问题:

void Main()
{
    var iq1 = ActionPlans.Select(ap => ap.ID);
    var iq2 …
Run Code Online (Sandbox Code Playgroud)

.net linq entity-framework exception visual-studio-2012

12
推荐指数
1
解决办法
3268
查看次数