Linq to EF Join throws “Index was out of range” after upgrade from VS2010 to VS2012

后端 未结 2 551
余生分开走
余生分开走 2021-02-14 00:19

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

2条回答
  •  不知归路
    2021-02-14 00:46

    After giving this some more investigation, I've come to the conclusion that the problem is the anonymous class I'm returning from the Linq query, I think it isn't allowed anymore to return an anonymous class with only one field in it, I know it isn't needed to wrap the field in an anonymous class but ... as I said this worked before upgrading.

    Following example gives me the "ArgumentOutOfRangeException - Index was out of range":

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

    this next example works as expected:

    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) => a );
    
        iq2.Dump();
    }
    

提交回复
热议问题