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

The Archive Base Latest Questions

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

I have a object model like this public class Parent{ public int Id; public

  • 0

I have a object model like this

public class Parent{
 public int Id;
 public string Name;
 public Child Sibling 
}

public class Sibling {
 public int Id;
 public string Name;
 public Cousin Cousin
}

public class Cousin{
 public int Id;
 public string Name;
 public DateTime CreatedDate;
}

I’m trying to flatten or project? it to a model like this

public class ViewModelSibling {
   public int Id;
   public string Name;
}
public class ViewModel{
 public int ParentId;
 public string ParentName;
 public ViewModelSibling Sibling;
 public ViewModelSibling Cousin;
 public DateTime? CousinCreatedDate;
}

where Sibling and Cousin are Nullable

ControllerCode

 [HttpPost]
 public virtual ActionResult GetGridItems()
 {
     IQueryable<Parent> parents = GetParentsWhereCriteriaMet();
     var data = parents.Select( p => new ViewModel{
                       ParentId = p.Id,
                       ParentName = p.Name,
                       Sibling = new ViewModelSibling  { Id = p.Sibling.Id, Name = p.Sibling.Name},// if p.Sibling is null and exception is thrown
                       Cousin= new ViewModelSibling  {Id =p.Sibling.Cousin.Id, Name = p.Sibling.Cousin.Name}// if p.Sibling or p.Sibling.Cousin are null and exception is thrown
                       CousinCreatedDate = p.Sibling.Cousin.CreatedDate
                       } )
     return new JsonResult { Data = data };
 }

I’m trying to avoid using ToList() because I don’t want all of the Parent records to be returned. The ViewModel is going to be bound to a Telerik Grid using Ajax, this handles the paging.
I don’t want to use AutoMapper because I don’t want all fields and records to be returned.

How do I flatten this model without using ToList()?

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

    Use either

    Sibling = p.Sibling == null ? null : new ViewModelSibling(...)
    

    or

    Sibling = new ViewModelSibling{ Id = p.Sibling == null ? 0 : p.Sibling.Id, etc ... }
    

    depending on your requirements.

    etc…

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

Sidebar

Related Questions

I have an object model like this: class Car { public string Manufacturer; public
I have an object model like this: public class MyClass { int TheProp {get;set;}
I have an object model somewhat like this: public class MyObject { public string
I have a object model a bit like this: public class Foo { public
I have an object model like this: public class Quantity { public decimal Weight
I have a model (simplified for relevance) like this: public abstract class TitleCreateModel :
I have a model like this defined... public class Product{ private long timestamp; //
I have an object model that looks like this (pseudo code): class Product {
I have some classes that look like this: MODEL public abstract class BaseEntity<O extends
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string

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.