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

I have just asked this question an hour ago but with regards to IE8
I have a problem similar to this question which was previously asked but my
I asked this question yesterday, and at the time it was just what I
Sorry for my bad English. Just like what I asked. I have this class
I just asked this question and the good answers mentioned using an initialization list.
I just asked this question Multiple Where conditions , but realised there was more
I have looked around here to see if somebody has asked this question before
I have asked this question countless times on various forums. Ultimately all I need
I have already asked this question on ServerFault however I got very minimal replies....one
I asked this question earlier, but I'm missing one important thing. I have a

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.