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

  • Home
  • SEARCH
  • 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 563329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:38:55+00:00 2026-05-13T12:38:55+00:00

I have two tables that I am using Linq to SQL with. The tables

  • 0

I have two tables that I am using Linq to SQL with. The tables have a 1 to many association. The important part of the database schema is as follows:

Camera:
  Id (int)
  SerialNumber (string)
  ...

CameraCalibration
  Id (int)
  CameraFk (int)
  ...

Using LINQ to SQL I can obtain a list of all Camera Calibrations for cameras with 10 character serial numbers as follows

var query = from n in db.CameraCalibrations
    where n.Camera.SerialNumber.Length == 10
    select n

From my understanding of LINQ the following query should equally work (even if by comparision it is rather brutal…)

var query = from n in db.CameraCalibrations
where db.Cameras.Where(c => c.Id == n.CameraFk).SingleOrDefault()
                .SerialNumber.Length == 10
select n

However when I execute this second query against the database I get a SQL exception stating “Cannot call methods on nvarchar”. When I look at the generated SQL it seems fairly clear why the exception is being generated:

SELECT t0.*
FROM CameraCalibration AS t0
WHERE (
    SELECT t1.serialNumber.Length
    FROM Camera AS t1
    WHERE t1.id = t0.cameraFk
    ) = 10

Notice how the generated SQL uses the expression t1.serialNumber.Length? I would have expected this to be translated into LEN(t1.serialNumber), and indeed with this change the query works.

Am I doing something wrong here? Or is this an error in the way I’ve structured my query and/or a limitation of LINQ to SQL?

Although I can easily restructure the query I’ve used to demonstrate the problem, in my real scenario that will be much harder (in part due to dynamically generated LINQ being involved).

How can I write a query similiar to the second one (with a lookup performed within the where clause) that LINQ to SQL will be happy to execute?

  • 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-13T12:38:55+00:00Added an answer on May 13, 2026 at 12:38 pm

    In this particular instance I think you need to join your two tables in your Linq statement. This will allow you to get an instance of the SerialNumber without using the .SerialNumber.Length association. Something like this (untested):

    var query = from n in db.CameraCalibrations 
                join c in db.Cameras on c.Id equals n.CameraFk
                where c.SerialNumber.Length == 10
                select n;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.