Cast errors when trying to return IQueryable <MyType>

I have a request that should return IQueryable<MyType>. The code is as follows:

public IQueryable<MyType> GetFooList()
{
    var query = (from x in dbContext.TableX
                 join y in dbContext.TableY on x.our_id equals y.our_id 
                 join z in dbContext.TableZ on y.our_id equals z.our_id 
                 join a in dbContext.TableA on z.other_id equals a.other_id 
                 where !string.IsNullOrEmpty(x.status)
                 select new
                 {
                   (fields....)
                 })
                 .AsQueryable();
    IQueryable<MyType> result = (IQueryable<MyType>) query;
    return result;
}

In the actions of the calling controller, I want to filter this list for the values ​​specified at runtime; The options for filtering for will differ between different callers. For example:.

List<MyType> FooList = Interface.GetFooList()
    .Where( specific conditions )
    .ToList();

An resultexception occurs in the line setup :

Invalid Cast exception was not handled by user code

"System.Data.Entity.Infrastructure.DbQuery'1 [< > f__AnonymousType9'9 [System.String, System.Nullable`1 [System.DateTime], System.String, System.String, System.String, System.String, System.Int32, System.Nullable'1 [System.DateTime], System.Nullable'1 [System.DateTime]]] " System.Linq.IQueryable'1 [MyType]".

, , , .Cast<MyType>() AsQueryable(). :

" " "MyType". LINQ to EDM- .

, , Entity Frameworks.

" ", - . .Select(obj => new MyType() {fields...} ), . .

, - .

, : select new MyType() {fields...}. . NotSupportedException, , :

'MyType' LINQ to Entities.

ETA2

EF MyTypeDTO. MyType MyTypeDTO. :

'our_id' LINQ to Entities. , .

DTO:

public int our_id { get; set; }

, get/set, rebuilt reer. , .

+4
2

. MyType. , .

, , . :

public IQueryable<MyViewModel> GetFooList(int parameter)
{
    var query = (from x in dbContext.TableX
                 join y in dbContext.TableY on x.our_id equals y.our_id
                 join z in dbContext.TableZ on y.our_id equals z.our_id
                 join a in dbContext.TableA on z.other_id  equals a.other_id 
                 where x.their_id == parameter
                 select new MyViewModel()
                 {
                    field1 = x.field1,
                    field2 = y.field2,
                    field3 = z.field3 //<= this field did not exist in MyType
                 }
                 ).AsQueryable();

    return query;
}
0

, , , , (.. , select new {...}) .

.Select(obj => new MyType() {fields...} ), . .

- , , MyType :

 var query = from x in dbContext.TableX
             join y in dbContext.TableY on x.our_id equals y.our_id 
             join z in dbContext.TableZ on y.our_id equals z.our_id 
             join a in dbContext.TableA on z.other_id equals a.other_id 
             where !string.IsNullOrEmpty(x.status)
             select new MyType
             {
                MyTypeField1 = a.Column1
             ,  MyTypeField2 = z.Column3
             ,  // ...and so on
             }; // No need to convert to IQueryable - this should produce the right type
return result;

'MyType' LINQ to Entities.

, MyType . , , EF . ( DTO).

MyTypeDto, MyClass, . MyClassDto . .

+8

All Articles