gpt4 book ai didi

linq - Linq 的奇怪行为

转载 作者:行者123 更新时间:2023-12-01 05:06:21 28 4
gpt4 key购买 nike

我有一个运行着 Sql Server CE 3.5 数据库的 Windows 窗体应用程序 (.NET 4.0),我通过 EF 连接访问它。

这是我的初始查询,它返回两个结果:

var list = db.UserPresentation
.Select(up => new
{
UserPresentationID = up.UserPresentationID,
PresentationName = up.PresentationName,
DateRequested = up.DateRequested,
Edit = string.Empty,
Delete = string.Empty,
Download = string.Empty
})
.OrderByDescending(up => up.DateRequested)
.ToList();

现在我引入了一个外部变量和一个 where 子句,它返回零结果。如果我在 LinqPad 中运行相同的代码,它会返回 2 个结果。

 int userID = 2;

// load list of user presentations
var list = db.UserPresentation
.Where(up => up.UserID == userID)
.Select(up => new
{
UserPresentationID = up.UserPresentationID,
PresentationName = up.PresentationName,
DateRequested = up.DateRequested,
Edit = string.Empty,
Delete = string.Empty,
Download = string.Empty
})
.OrderByDescending(up => up.DateRequested)
.ToList();

现在我在查询中硬编码 userid,它再次返回两个结果:

var list = db.UserPresentation
.Where(up => up.UserID == 2)
.Select(up => new
{
UserPresentationID = up.UserPresentationID,
PresentationName = up.PresentationName,
DateRequested = up.DateRequested,
Edit = string.Empty,
Delete = string.Empty,
Download = string.Empty
})
.OrderByDescending(up => up.DateRequested)
.ToList();

我真的很难过。知道这里发生了什么吗?

最佳答案

UserID 可以为空吗?

如果是,请务必执行 .Where(up => up.UserID.HasValue && up.UserID.Value == userID)

我曾经有过类似的可空日期时间

关于linq - Linq 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8092911/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com