简体   繁体   中英

In entity framework 6 SelectMany to Join option for query throwing error The source IQueryable doesn't implement IDbAsyncEnumerable<MyModel>

I have a following query throwing error as mentioned in subject

public async Task<IEnumerable<MyModel>> GetDataAsync()
{
    return await _context.baseTable.SelectData().AsNoTracking().ToListAsync();
}
    
public static IQueryable<MyModel> SelectData(this IQueryable<relet> baseTable)
{
    return baseTable
        .Where(h => h.table1 != null && h.table1.table2 != null)
        .Select(lev => new {
            table3Collection = lev.table1.table2.table3, //table3 is ICollection
            Table5Key = lev.table5 != null ? lev.table5.id : null,
            Table4Key = lev.table1.table2.table4 != null ?  lev.table1.table2.table4.id : null,
        }).ToList()
        .SelectMany(x => {
            return x.table3Collection.Select(h => new MyModel() {
                ContactKey = new Key
                {
                    Id = h.Id,
                    Type = h.Type
                },
                Table5Key = x.Table5Key,
                Table4Key = x.Table4Key
    
            });
        })
        .AsQueryable();
}

Can someone Give me idea how to use same LINQ query in group join way? Problem is occuring at .ToListAsync() line

You cannot create IQueryable from IEnumerable and use asynchronous extensions. Actually you just need to correct your query and remove unnecessary materializations.

public static IQueryable<MyModel> SelectData(this IQueryable<relet> baseTable)
{
    return 
        from lev in baseTable
        from t3 in lev.table1.table2.table3
        select new MyModel
        {
            ContactKey = new Key
            {
                Id = lev.Id,
                Type = lev.Type
            },
            Table5Key = (int?)lev.table5.Id,
            Table4Key = (int?)lev.table1.table2.table4.Id
        };
}

Note that from simplified query we can see that t3 is not used. It means that we just duplicate records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM