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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:09:29+00:00 2026-05-31T14:09:29+00:00

How can I query the DB to return a list of items that have

  • 0

How can I query the DB to return a list of items that have fields in addition to what the entity models describe>

Say I have entity models called Owners and Pets:

public class Owner
{
    public int OwnerID{ get; set; }

    public virtual IEnumerable<Pets> Pets{ get; set; }
}

public class Pet
{
    public int PetID{ get; set; }
    public string PetName{ get; set; }

    public int OwnerID{ get; set; }
    public Owner Owner{ get; set; }
}

If I got a list of owners like this:

dbContext.Set<Owner>().ToList()

Each object in that list would be an Owner with OwnerID and a list of their pets.

But, what if I wanted to include an extra field for each of those Owners, like “HasPets”, and have a list of viewmodels that accepts that.

public class OwnerViewModel
{
    public int OwnerID{ get; set; }
    public bool HasPets{ get; set; }
}

List<OwnerViewModel> OwnerViewModels = ....(get list with added field)

I have no idea how to query the DB to include a new calculated field…

(I realize I could simply do a count on Owner.Pets, but I just wanted to keep the example simple)

  • 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-31T14:09:30+00:00Added an answer on May 31, 2026 at 2:09 pm

    Something like

    context.Owners.Select(o => new OwnerViewModel() 
                               {Owner = o, HasPets = o.Pets.Any()});  
    

    And define your viewmodel like:

    public class OwnerViewModel
    {
        public Owner Owner{ get; set; }
        public bool HasPets{ get; set; }
    }
    

    Or you can load all the pets client-side, and check them there (of course not recommended for the overload):

    public class OwnerViewModel
    {
        public Owner Owner{ get; set; }
        public bool HasPets{ get { return this.Owner.Pets.Any(); } }
    }  
    var ownervms = new List<OwnerViewModel>();
    foreach (var owner in context.Owners)
    {
        ownervms.Add(new OwnerViewModel() {Owner= owner});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I return the rows deleted as objects? Would I have to query
I have a list of items that is refreshed daily where each item falls
I have a custom aggregate function that concatenates items with a delimiter. How can
How can I write a LINQ query that returns an hierachical anonymous type from
I can query for the picture,location and privacy fields/connections of a users Facebook event
I am having trouble writing query so that I can query the content of
I have an OData service running on top of Oracle database. I can query
I'm using C# 3.5 and EntityFramework. I have a list of items in the
I am writing a program that will query an MS access database, return the
Let's say that you are limited to use only this syntax (you can't use

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.