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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:52:58+00:00 2026-05-31T07:52:58+00:00

In my database I have a customer, who can have multiple locations and a

  • 0

In my database I have a customer, who can have multiple locations and a location can have multiple customers. I have dragged my database design into a Linq To SQL dataclasses file and this is how it currently looks.

database

In my form I am trying to retrieve the adress (or multiple) from a customer.
I have tried the following code but I always get the same error: Sequence contains no elements, while it does have elements! (Customer with ID 2, Location with ID 1 and the join table with 2,1)

    public static locatie getLocationByCustomer(int id)
    {
       var query = (from v in dc.locaties
                      from e in v.locatie_klants
                      where e.klant_id == id
                      select v);


        locatie locatie = query.First();
        return locatie;
    }

Translations: klant –> Customer || locatie –> Location || locatie_klant –> location_customer

I’m not sure what I’m doing wrong here.
I’m also wondering how I would save a new customer to the database (with multiple locations), any ideas?

Thanks for the help!

  • 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-31T07:52:58+00:00Added an answer on May 31, 2026 at 7:52 am

    Apparently the customer your querying is not related to any location. Therefore your query does not return any elements. This itself is not an issue, but in your sample you are using First() rather than FirstOrDefault().

    Why is this an issue:

    • First() will throw an exception if there is no address for the given client id.
    • FirstOrDefault() will return null if there is no address for the given client id.

    Especially with relations like these you should stick to FirstOrDefault() since you can never be sure that there is a relation present for the items you are querying.

    Here is a sample on how to add a Customer + CustomerLocations:

    var newCustomer = new klant()
    {
        // set neccessary properties
    };
    
    var newCustomerLocation1 = new locatie_klant()
    {
        // set neccessary properties
    };
    
    var newCustomerLocation2 = new locatie_klant()
    {
        // set neccessary properties
    };
    
    using (var context = new DBContext())
    {
        context.klants.Add(newCustomer);
        context.locatie_klants.Add(newCustomerLocation1);
        context.locatie_klants.Add(newCustomerLocation2);
        context.SaveChanges():
    }
    

    Some general advice:
    Stick to English names for development objects. This is considered best practice also makes your life easier when using questions on so / forums.

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

Sidebar

Related Questions

I have customer database who have certain attributes, and a customer type. The collection
We have customers who are upgrading from one database version to another (Oracle 9i
I have a customer that has set up a full backup of the database
I have a customer that has an application that uses an Advantage Database. I
I have a database full of customer data. It's so big that it's really
I have a database for tracking customer purchases over history. One of my forms
Suppose I have a class Customer that is mapped to the database and everything
I have a simple database named customer with a single table data.I want to
I have a web database app that runs in IE7+ (customer requirement) It is
I have two tables in my database that look like that: Customer: C_ID city

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.