Entity framework Efcore 2.2-where子句在选择后运行并返回错误结果
我有一个简单的疑问:Entity framework Efcore 2.2-where子句在选择后运行并返回错误结果,entity-framework,asp.net-core,entity-framework-core,ef-core-2.2,Entity Framework,Asp.net Core,Entity Framework Core,Ef Core 2.2,我有一个简单的疑问: Expression<Func<Tips, bool>> lastTipsPredicate = x => x.Status == (int)EnumGringo.LU_Status.active; IQueryable<Tips> lastQueryBase(DbSet<Tips> t) => t.OrderByDescending(x => x.CreateDate).Take(6); IEnumera
Expression<Func<Tips, bool>> lastTipsPredicate = x => x.Status == (int)EnumGringo.LU_Status.active;
IQueryable<Tips> lastQueryBase(DbSet<Tips> t) => t.OrderByDescending(x => x.CreateDate).Take(6);
IEnumerable<Tips> latestTips = await base.GetAllByCondition(lastTipsPredicate, lastQueryBase);
它只返回5条记录,而不是我预期的6条,1条记录被过滤掉,因为它有状态=一,
虽然此查询是正确的,并返回最后6条记录:
SELECT top 6 [t].[ID], [t].[CreateDate], [t].[Description], [t].[InsertedByGringo], [t].[IsRecommended], [t].[IsSubscribe], [t].[LanguageType], [t].[SeoId], [t].[Slug], [t].[Status], [t].[Title], [t].[UserID], [t].[ViewCount]
FROM Tips as [t]
WHERE [t].[Status] = 1
ORDER BY [t].CreateDate DESC
如何生成第二个查询而不是第一个查询
Efcore 2.2-where子句在选择后运行并返回错误结果
这既不是EF核心问题,也不是LINQ问题,而是存储库方法构建LINQ查询的方式
如果要先应用筛选(Where
),然后可选地应用其余部分,则应将baseQuery
func输入类型从DbSet
更改为IQueryable
,实现如下:
公共虚拟异步任务GetAllByCondition(
表达式谓词,
Func baseQuery=null)
{
var q=context.Set()
.Where(谓词)/@Thangadurai lol,第一个查询选择最后6条记录并删除1,我留下5条。第二个查询将返回状态为1的最后6条记录。谢谢你,伊万,你又一次得到了正确的答案,你能提供任何参考或解释DbSet和IQueryable之间的区别吗?与查询standp的区别要点是
DbSet
实现了(因此是一个)IQueryable
,而反之则不成立。参考文献:,
exec sp_executesql N'SELECT [t].[ID], [t].[CreateDate], [t].[Description], [t].[InsertedByGringo], [t].[IsRecommended], [t].[IsSubscribe], [t].[LanguageType], [t].[SeoId], [t].[Slug], [t].[Status], [t].[Title], [t].[UserID], [t].[ViewCount]
FROM (
SELECT TOP(@__p_0) [x].[ID], [x].[CreateDate], [x].[Description], [x].[InsertedByGringo], [x].[IsRecommended], [x].[IsSubscribe], [x].[LanguageType], [x].[SeoId], [x].[Slug], [x].[Status], [x].[Title], [x].[UserID], [x].[ViewCount]
FROM [Tips] AS [x]
ORDER BY [x].[CreateDate] DESC
) AS [t]
WHERE [t].[Status] = 1',N'@__p_0 int',@__p_0=6
SELECT top 6 [t].[ID], [t].[CreateDate], [t].[Description], [t].[InsertedByGringo], [t].[IsRecommended], [t].[IsSubscribe], [t].[LanguageType], [t].[SeoId], [t].[Slug], [t].[Status], [t].[Title], [t].[UserID], [t].[ViewCount]
FROM Tips as [t]
WHERE [t].[Status] = 1
ORDER BY [t].CreateDate DESC