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

The Archive Base Latest Questions

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

I have a cart model class that has a List property like so: public

  • 0

I have a cart model class that has a List property like so:

public List<CartItem> CartItems
{
    get
    {
        if (_cartItems == null)
            _cartItems = Services.CartItemService.GetCartItems();

        return _cartItems;
    }
}
private List<CartItem> _cartItems;

This is fine, unless the service that’s used to query the data from SQL Server returns null, in which case, the database can be unnecessarily hit multiple times as CartItems is referenced. I then noticed that Lazy<T> was available to me, so I attempted to modify my code slightly (since Lazy<T> accounts for null and would prevent multiple hits to the database)

public List<CartItem> CartItems
{
    get
    {
        return _cartItems.Value;
    }
}

private Lazy<List<CartItem>> _cartItems = new Lazy<List<CartItem>>(() =>
{
    // return Services.CartItemService.GetCartItems(); cannot be called here :(
});

The compile time error is

“A field initializer cannot reference the non-static field, method, or
property”

Services is a public property in the same class as CartItems but I can’t figure out if it’s even possible to access that in a Func<List<CartItem>> delegate. I do not want to have to create factory classes for each property – I have to us something like this in many places, and I want to be … well…. lazy.

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

    You can create the field in a constructor.
    It might also pay to move the service call to it’s own method.
    I.e.

    private readonly Lazy<List<CartItem>> _cartItems;
    
    public MyClass()
    {
        _cartItems = new Lazy<List<CartItem>>(GetCartItems);
    }
    
    public List<CartItem> GetCartItems()
    {
        return Services.CartItemService.GetCartItems();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sample code like this: <div class=cart> <a onclick=addToCart('@Model.productId'); class=button><span>Add to Cart</span></a> </div>
I have a user model that has many carts class User < ActiveRecord::Base has_many
I have the following model (simplified): abstract class CartItem { EntityReference<Cart> Cart; } class
I have following model: class Customer < ActiveRecord::Base has_one :cart end class Cart <
I have a users_manager engine which has a User model class. In an other
I have a simple cart page that displays items that are in someones cart,
I have function getCartItems in cart.js and I want to call that function in
I have an Action that basically adds an item to a cart, the only
Here is my problem. I have a list of models that are displayed to
Here is my problem. I have a list of models that are displayed 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.