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 8066663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:04:46+00:00 2026-06-05T12:04:46+00:00

Spurred on by this question , I decided to try this in my MVC3/ActiveRecord

  • 0

Spurred on by this question, I decided to try this in my MVC3/ActiveRecord application.

I have a bunch of models already working, and some views that do stuff with those models. Nothing special here. One of my models is called AppSession.

Based on said question, I expected this to work: AppSession.FirstOrDefault(a => ...) ?? null.

It didn’t work. I still get an InvalidOperationException. This is true of FirstOrDefault and SingleOrDefault. I ended up wrapping my call in try/catch to get around it.

What am I doing wrong here?

Edit: As requested, the actual code is:

void getAnAppSession() {
    AppSession sessions = project.AppSessions.FirstOrDefault(a => a.Ip == ip && a.MacAddress == macAddress && a.Platform == platform && a.Version == version && a.EndTime == null)
}

ip, macAddress, platform, and version are all method variables that are verifiably not null. My schema for AppSessions (and accordingly, properties on my class) includes:

  • ID (int, not null)
  • StartDate (DateTime, not null)
  • EndDate (DateTime, null)
  • Ip (string, not null)
  • MacAddress (string, not null)
  • Platform (string, not null)
  • Version (string, not null)
  • 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-06-05T12:04:47+00:00Added an answer on June 5, 2026 at 12:04 pm

    Maybe your project.AppSessions itself is null? That would cause the FirstOrDefault() method to throw an error. You might want to check if that is null before calling FirstOrDefault and create a new AppSession object if it is null:

    AppSession sessions = (project.AppSessions as AppSession ?? new AppSession())
        .FirstOrDefault(a => a.Ip == ip && 
                        a.MacAddress == macAddress && 
                        a.Platform == platform && 
                        a.Version == version && 
                        a.EndTime == null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question Add a version number to the title of a LaTeX document spurred
My Google-fu is failing me on this question. I have a coworker who has
Simple question, spurred on by the removal of the target attribute in HTML 4.0
Spurred on by success stories of developers using VMs I've decided to give it
I have a situation where in some cases a dropdownlist can contain a default
There are a lot of people out there against the use of public/private static
The following JSON is not passing validation. The validator complains at the first string
I must be really stupid, but I'm at my wits' end with a JPA
In Delphi, I want to be able to create an private object that's associated

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.