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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:04:07+00:00 2026-05-20T12:04:07+00:00

I am having some trouble with a linq query I am trying to write.

  • 0

I am having some trouble with a linq query I am trying to write.

I am trying to use the repository pattern without to much luck. Basically I have a list of transactions and a 2nd list which contains the description field that maps against a field in my case StoreItemID

    public static IList<TransactionViewModel> All()
    {
        var result = (IList<TransactionViewModel>)HttpContext.Current.Session["Transactions"];
        if (result == null)
        {
            var rewardTypes = BusinessItemRepository.GetItemTypes(StoreID);
            HttpContext.Current.Session["Transactions"] = 
                result =
              (from item in new MyEntities().TransactionEntries
                 select new TransactionViewModel()
                                   {
                            ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID).ItemDescription,
                     TransactionDate = item.PurchaseDate.Value,
                     TransactionAmount = item.TransactionAmount.Value,
                                   }).ToList();
        }
        return result;
    }


     public static List<BusinessItemViewModel>GetItemTypes(int storeID)
    {
        var result = (List<BusinessItemViewModel>)HttpContext.Current.Session["ItemTypes"];
        if (result == null)
        {
            HttpContext.Current.Session["ItemTypes"] = result =
                (from items in new MyEntities().StoreItems
                 where items.IsDeleted == false && items.StoreID == storeID
                 select new BusinessItemViewModel()
                 {
                     ItemDescription = items.Description,
                     StoreID = items.StoreID,
                     StoreItemID = items.StoreItemID
                 }).ToList();
        }
        return result;

However I get this error

Unable to create a constant value of type ‘MyMVC.ViewModels.BusinessItemViewModel’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.

I know its this line of code as if I comment it out it works ok

ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID).ItemDescription,

How can I map ItemDescription against my list of itemTypes?

Any help would be great 🙂

  • 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-20T12:04:08+00:00Added an answer on May 20, 2026 at 12:04 pm

    This line has a problem:

     ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID)
                                .ItemDescription,
    

    Since you are using FirstOrDefault you will get null as default value for a reference type if there is no item that satifies the condition, then you’d get an exception when trying to access ItemDescription – either use First() if there always will be at least one match or check and define a default property value for ItemDescription to use if there is none:

     ItemDescription = itemTypes.Any(r=>r.StoreItemID==item.StoreItemID) 
                               ? itemTypes.First(r=>r.StoreItemID==item.StoreItemID)
                                          .ItemDescription
                               : "My Default",
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.