该问题最初是在http://entityframework.codeplex.com/discussions/399499#post928179上提出的.
美好的一天!请告诉我发布此问题是否错误.
我有一个查询如下:
IQueryable<Card> cardsQuery =
dataContext.Cards
.Where(predicate)
.OrderByDescending(kc => kc.SendDate)
.AsQueryable();
Run Code Online (Sandbox Code Playgroud)
然后我尝试:
Task<Card[]> result = cardsQuery.ToArrayAsync();
异常上升:
The source IQueryable doesn't implement IDbAsyncEnumerable<Models.Card>
我使用'EF 5.x DbCotext generator'的修改版本.
怎么避免呢?
UPDATE
重要的是我有生产方法IQuerayble<Card>如下:
class Repository {
public IQueryable<Card> GetKudosCards(Func<Card, bool> predicate) {
IEnumerable<KudosCard> kudosCards = kudosCardsQuery.Where(predicate);
return kudosCards
.OrderByDescending(kc => kc.SendDate)
.AsQueryable();
}
}
Run Code Online (Sandbox Code Playgroud)
调用AsQueryable有什么意义?如果使用从IQueryable源集合(例如DbSet,ObjectSet)开始的扩展方法编写查询,则查询也将是IQueryable.
AsQueryable的目的是使用IQueryable代理/适配器包装IEnumerable集合,该代理/适配器使用能够将IQueryable查询编译为Linq to Object查询的Linq提供程序.当您想要使用内存数据查询时,这可能很有用.
为什么AsQueryable调用是必要的?如果你只是删除它怎么办?
更新
哦,现在看来我理解你的问题了.快速浏览一下ODataQueryOptions.ApplyTo后,我意识到它只是扩展了查询的底层表达式树.您仍然可以使用它以您想要的方式运行查询,但是您需要一个小技巧来将查询转换回泛型.
IQueryable<Card> cardsQuery =
dataContext.Cards
.Where(predicate)
.OrderByDescending(kc => kc.SendDate);
IQueryable odataQuery = queryOptions.ApplyTo(cardsQuery);
// The OData query option applier creates a non generic query, transform it back to generic
cardsQuery = cardsQuery.Provider.CreateQuery<Card>(odataQuery.Expression);
Task<Card[]> result = cardsQuery.ToArrayAsync();
Run Code Online (Sandbox Code Playgroud)
小智 8
当我使用 LinqKit 库表达式生成器时,我遇到了同样的问题,它最终生成了 ,AsQueryable()令人惊讶的是,它在 XUnit 集成测试调用中发生了。
我很想知道为什么通过 Swagger 调用相同的 API 端点时没有发生同样的问题。
结果我必须做一个基本的改变。我不得不更换:
using System.Data.Entity;
Run Code Online (Sandbox Code Playgroud)
和:
using Microsoft.EntityFrameworkCore;
Run Code Online (Sandbox Code Playgroud)
问题如下。
我有一个方法:
class Repository {
public IQueryable<Card> GetKudosCards(Func<Card, bool> predicate) {
IEnumerable<KudosCard> kudosCards = kudosCardsQuery.Where(predicate);
return kudosCards
.OrderByDescending(kc => kc.SendDate)
.AsQueryable();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是 kudosCards 的类型是IEnumerable<KudosCard>。这会引发异常。如果我将谓词类型更改为,Expression<Func<Card, bool> predicate那么一切都会正常工作。
| 归档时间: |
|
| 查看次数: |
10283 次 |
| 最近记录: |