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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:33:40+00:00 2026-06-04T02:33:40+00:00

I am trying to populate an instance with LINQ containing an array of nested

  • 0

I am trying to populate an instance with LINQ containing an array of nested classes. I have managed to do this with the following LINQ. I have also included the classes that make up the instance in the select.

    select new EditableWarrantBook {
        Id = p1.id,
        Comment = p1.EntryComment,
        WarrantYear1 = new BookYear {
            StatusYear = p2.StatusYear,
            Status = p2.Status,
        },
        WarrantYear2 = new BookYear {
            StatusYear = p3.StatusYear,
            Status = p3.Status,
        },
        WarrantYear3 = new BookYear {
            StatusYear = p4.StatusYear,
            Status = p4.Status,
        }
    }

public class EditableWarrantBook
{
    public int Id { get; set; }
    public string Comment { get; set; }

    public BookYear[] WarrantYear = new BookYear[3];

    public BookYear WarrantYear1
    {
        get { return WarrantYear[0]; }
        set { WarrantYear[0] = value; }
    }
    public BookYear WarrantYear2
    {
        get { return WarrantYear[1]; }
        set { WarrantYear[1] = value; }
    }
    public BookYear WarrantYear3
    {
        get { return WarrantYear[2]; }
        set { WarrantYear[2] = value; }
    }

}

public class BookYear
{
    public int? StatusYear { get; set; } 
    public string Status { get; set; } 
}

This works I can access values with either WarrantYear[0] or WarrantYear1. This can sometimes be useful when designing UI. However, in this case, I don’t need the WarrantYear1 property because I am turning this into JSON and I don’t need to repeat (or want to send two versions of the same data on the network). My question is, how do I write the select statement to load WarrantYear array. Or how do I write the class so that I can access the array as a property. My solution should not contain the Warrant1, Warrant2, Warrant3 properties in the EditableWarrantBook Class.

  • 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-04T02:33:41+00:00Added an answer on June 4, 2026 at 2:33 am

    The problem is that you’re trying to mix SQL world (building your query) with your CLR world (creating new objects). The way to get around this is to break up your queries into your initial data fetching query (SQL world), and then enumerate over the results to transform into your objects (CLR world):

    So after the where p1.SaleYear == 2009, add this:

    select new { p1, p2, p3, p4 })
    .AsEnumerable()
    .Select(tr => new EditableWarrantBook
        {
        Id = tr.p1.id,
        Comment = tr.p1.EntryComment,
        WarrantYear = new[] {
            tr.p2 == null ? (BookYear)null : new BookYear // EDITED LINE
            {
                StatusYear = tr.p2.StatusYear,
                Status = tr.p2.Status,
            },
            tr.p3 == null ? (BookYear)null : new BookYear // EDITED LINE
            {
                StatusYear = tr.p3.StatusYear,
                Status = tr.p3.Status,
            },
            tr.p4 == null ? (BookYear)null : new BookYear // EDITED LINE
            {
                StatusYear = tr.p4.StatusYear,
                Status = tr.p4.Status,
            }}
        }).ToList();
    

    If you want to stay in query syntax, then just change it to:

    result = from tr in 
        (/*Select up to the AsEnumerable call above)*/).AsEnumerable()
        select new EditableWarrantBook
            {
                /* Initialization code supplied above */
            }
    

    Edit: So I’ve modified the example one more time. It looks like one of the values that you’re getting back is null, so I’ve added a null check to make sure you’re not accessing properties of null objects (although I didn’t check p1 for null).

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

Sidebar

Related Questions

I have a simple class that I trying to populate from an XML document.
I have a DataGridView that I'm trying to populate using a For loop: Dim
I have a string array defined as String[] sEmails; that I am trying to
I am trying populate H3 with the sum of column E for rows that
i'm trying to populate a ViewData instance with html markup like shown below. When
I have 2 1d arrays and im trying to populate them into a single
what i'm trying to do is walk an object that is also a complex
I'm trying to pass my View an instance of the following ViewModel: public class
I have two data structure classes (this is a simplified version of my code)
I have 2 classes for a UITableViewCell that show different data but they 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.