Need to cast explicitly thru anonymous type in Union()

后端 未结 2 1494
我在风中等你
我在风中等你 2021-01-20 01:11

I have 2 var/objects, retrieving thru these 2 functions:

private IQueryable SelectAll_1(...)
{
    return query;
}

class Pro

2条回答
  •  情话喂你
    2021-01-20 01:15

    The problem is that P2 is an IQueryable while P3 is an IQueryable and you can't union them into one sequence, since they are of different type.

    What you need to do is to project both of them into one common type with a Select() call which as far as I know must be a named type according to Jon Skeet can be done with an anonymous type. The initialization blocks that define the anonymous types must have the same number of members, with the same name, in the same order, with the same types to be treated as one type. To project the types with Select() you also need to make the properties public to be accessible for the linq methods.

提交回复
热议问题