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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:40:15+00:00 2026-06-17T21:40:15+00:00

I have two models Garden and Flower public class Garden { public int Id

  • 0

I have two models Garden and Flower

public class Garden
{
    public int Id { get; set; }
    public string Name { get; set; } // Default value: Garden #[Id]

    // Location.
    public int LocationX { get; set; }
    public int LocationY { get; set; }

    // Flowers.
    public virtual List<Flower> Flowers { get; set; }
}

public class Flower
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
    public double Height { get; set; }

    public DateTime LastWatered { get; set; }
    public DateTime Planted { get; set; }
}

public class DataContext : DbContext
{
    public DbSet<Garden> Gardens { get; set; }
}

And now, I need to select a flower by Id. I got to this, but I’m not sure that’s the good way. How should I do it? Do you have any articles about that?

var garden = _db.Gardens.Where(g => g.Id == gardenId).SingleOrDefault();
var flower = garden.Flowers.Where(f => f.Id == flowerId).SingleOrDefault();
  • 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-17T21:40:16+00:00Added an answer on June 17, 2026 at 9:40 pm

    I would suggest this:

     var garden = _db.Gardens.SingleOrDefault(g => g.Id == gardenId);
    
     Flower flower = null;
    
     if (garden != null)
     {
         flower = garden.Flowers.SingleOrDefault(f => f.Id == flowerId);
     }     
    

    Instead of using Where first and SingleOrDefault afterwards just use SingleOrDefault.

    EDIT

    As @JoachimIsaksson suggested, I have added a null reference check for garden to avoid the possible NullReferenceException.

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

Sidebar

Related Questions

I have two models: public class ProfessorModels { public string FullName { get; set;
I have two models: public class Resort { public int ID { get; set;
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models with relation: class Model1 extends CActiveRecord public function relations() {
I have two models such that class JobTitle(models.Model): name = models.CharField(max_length=1000) class Employer(models.Model): jobtitle
I have two models for store and city: class City(models.Model): name = models.CharField() slug
I have two models. @Entity public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) protected long id;
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models categories(id, name, slug) places(id, name, cat_id) when i have to

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.