无法分配 Nullable到 Linq.Table.Max()?
我的程序中有以下两行代码。目标是返回“值”字段中的最大数字。
MyDataContext context = new MyDataContext();
long? id = (long?)context.MyTable.Max(x => x.Value);
Value 字段是表中的 bigint 字段。该表也不能保证它包含行,事实上这是我现在正在测试的场景。我试图在 MyTable 没有行时运行它。
第二行返回无效操作异常。
不能将 null 值分配给 System.Int64 类型的成员,该类型是不可为 null 的值类型。
我尝试了几种不同的方法,但无法使其发挥作用。我显然不理解 C# 中可空类型的一些重要内容。
编辑(解决方案):
感谢克里斯的回答,我能够找到问题的实际原因和解决方案。事实证明,并不是 Value 字段为空,而是因为 lambda 表达式内的 x 是空行。因此,我在 lambda 内部添加了一个检查,以查看 x 是否为空,如果不是,那么我可以安全地检查 Value 是否为空。
MyDataContext context = new MyDataContext();
long? id = context.MyTable.Max(x => x == null ? (long?)0 : (long?)x.Value);
I have the following two lines of code in my program. The goal is to return the highest number from the Value field.
MyDataContext context = new MyDataContext();
long? id = (long?)context.MyTable.Max(x => x.Value);
The Value field is a bigint field in the table. The table also cannot guarantee that it contains rows, in fact that it is scenario I am testing now. I am attempting to run this when MyTable has no rows.
The second line returns an invalid operation exception.
The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type.
I've tried it several different ways but I am unable to get this to work. I obviously not understanding something important about nullable types in C#.
EDIT (solution):
Thanks to Chris's answer I was able to find the actual cause of my problem and solution. It turns out that it wasn't so much that the Value field was null but that since x inside the lambda expression was a null row. So I added a check inside the lambda to see if x was null, if not then I can safely check if Value is null.
MyDataContext context = new MyDataContext();
long? id = context.MyTable.Max(x => x == null ? (long?)0 : (long?)x.Value);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 max 函数尝试返回 Int64。您使用的 Max 重载在 MSDN 上定义。您会看到它需要一个参数
Func选择器。这是您的 lambda 表达式,表示您将给它一个 TSource,它将返回一个 long(即 Int64)。你的函数正在做的是当 x.Value 为 null 时返回一个 null 值,这当然对于 Int64 来说是无效的。
在这种情况下,您应该能够使用类似的内容:
这意味着如果 x 有一个值,它将返回该值,否则返回最小可能值,只有当所有内容都为空时,该值才会成为最大值。
我应该否认这段代码未经测试,但应该给你正确的想法。
编辑:我最初假设这是一个可为空的长整型,但事实证明有重载支持这一点,所以我现在假设 x 不是一个可为空的长整型,如果你眯着眼睛看的话,它看起来有点像一个int 呃正确的方法。
Your max function is trying to return an Int64. The overload of Max you are using is defined here on MSDN. You'll see that it takes a parameter
Func<TSource, long> selector
. This is your lambda expression and says that you'll give it a TSource and it will return a long (ie Int64). What your function is doing is when x.Value is null returning a null value which is of course not valid to as an Int64.In this case you should be able to use something like:
This means that if x has a value it returns that and otherwise returns the minimum possible value which will only ever be the max if everything is null.
I should disclaim that this code is untested but should give you the right idea.
edited: I originally was assuming that this was a nullable long but it turns out there are overloads that support that so I now assume that x is not a nullable long, just looks a bit like one if you squint int eh right way.