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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:24:37+00:00 2026-05-10T19:24:37+00:00

I just asked this question . Which lead me to a new question :)

  • 0

I just asked this question. Which lead me to a new question 🙂

Up until this point, I have used the following pattern of selecting stuff with Linq to SQL, with the purpose of being able to handle 0 ‘rows’ returned by the query:

var person = (from p in [DataContextObject].Persons               where p.PersonsID == 1               select new p).FirstOrDefault();  if (person == null) {     // handle 0 'rows' returned. } 

But I can’t use FirstOrDefault() when I do:

var person = from p in [DataContextObject].Persons              where p.PersonsID == 1              select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode };  // Under the hood, this pattern generates a query which selects specific // columns which will be faster than selecting all columns as the above // snippet of code does. This results in a performance-boost on large tables. 

How do I check for 0 ‘rows’ returned by the query, using the second pattern?

UPDATE:

I think my build fails because I am trying to assign the result of the query to a variable (this._user) declared with the type of [DataContext].User.

this._user = (from u in [DataContextObject].Users               where u.UsersID == [Int32]               select new { u.UsersID }).FirstOrDefault(); 

Compilation error: Cannot implicitly convert type ‘AnonymousType#1’ to ‘[DataContext].User’.

Any thoughts on how I can get around this? Would I have to make my own object?

  • 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. 2026-05-10T19:24:38+00:00Added an answer on May 10, 2026 at 7:24 pm

    Regarding your UPDATE: you have to either create your own type, change this._user to be int, or select the whole object, not only specific columns.

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

Sidebar

Related Questions

Can someone explain to me why this code prints 14? I was just asked
A related question to one I asked earlier ... Just checking on something: Should
My employer just asked me to run a timed batch process in a Java
Someone at work just asked for the reasoning behind having to wrap a wait
I'm a .NET web developer who has just been asked to produce a small
I was just browsing a forum and someone asked about a PHP file they
I was asked to make some drools traning for my teammates. Just to show
I just installed Oracle Express edition. During the installation it asked me to set
Just what the title says, I need to change the password for an existing

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.