Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:11:07+00:00 2026-05-26T06:11:07+00:00

I have the following two lines of code in my program. The goal is

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:11:07+00:00Added an answer on May 26, 2026 at 6:11 am

    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:

    x=> x.Value!=null?x.Value:Int64.Minvalue
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem in the following two code lines NSDictionary* last = [[arrHistory
I have the following two columns: SELECT b.ip_address AS IP ,b.mask AS MASK FROM
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have the following two classes, one inherits from the other Class A{ void
Say I have a file, t.txt, that contains the following two lines: one two
In a Javascript program I have an object with the following two (simplified) functions:
I have the following program where two variables are to be passed by reference
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
I have the following two projects in in Flex Builder 3: One AS3 library
I have the following two tables: Customer { int Id int Name } Bills

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.