LINQ到EF加入抛出"指数超出范围"升级从VS2010到VS2012后抛出、范围、指数、LINQ

2023-09-04 02:05:48 作者:玩世不恭

从Visual Studio 2010升级到2012 code后开始抛出ArgumentOutOfRangeException - 索引超出范围必须是非负数且小于collection.Parameter名的大小:指数。使用加入LINQ查询

After upgrading from Visual Studio 2010 to 2012 code started throwing "ArgumentOutOfRangeException - Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index" on Linq queries using Joins.

在LINQPad发(使用EF数据模型)下面简单的例子给了我ArgumentOutOfRangeException:

The following simple example made in LINQPad (using an EF data model) gives me the 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(); 
}

更改previous例如像预期的那样返回包含联接不给ArgumentOutOfRangeException并给出结果双方一个匿名对象:

Changing the previous example to return an anonymous object containing both sides of the join doesn't give the ArgumentOutOfRangeException and gives results as expected:

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(); 
}

好了,由于某种原因,我不得不返回的加盟两侧,但后来我尝试下面的例子使用的是虚值而不是,也执行没有问题:

Ok, so for some reason I had to return both sides of the join, but then I tried the following example using a dummy value instead, that also executed without a problem:

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, x = 1 });

    iq2.Dump(); 
} 

以第一个例子和加入了ToList()到第一查询也使得它不会有问题执行:

Taking the first example and adding a ToList() to the first query also makes it execute without a problem:

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

    iq2.Dump(); 
}

重要:试图在工作站上的第一个查询,而无需在Visual Studio 2012的升级工作正常

Important: Trying the first query on a workstation without the Visual Studio 2012 upgrade works fine!

任何人能确认/解释这个新的功能? : - )

Can anyone confirm/explain this new "feature"? :-)

推荐答案

欧文,只​​是关闭循环的:我们已经证实,这是我们在LINQ最近推出以实体错误,我们正在寻找方法来获得一个解决了。非常感谢报告吧!

Erwin, just to close the loop on this: We have confirmed that it is a bug we introduced recently in LINQ to Entities and we are looking at ways to get a fix out. Thanks a lot for reporting it!