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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:20:18+00:00 2026-06-16T10:20:18+00:00

Hi I am trying to convert a a LINQ query result into a list

  • 0

Hi I am trying to convert a a LINQ query result into a list of objects and I seem to be doing something wrong because I can not acces the properties of each object.Here is my code:

List<Object> productList = new List<Object>();

        var products = (from p in Products
                       join s in SubCategories on p.SubcatId equals s.SubCatId
                       join b in Brands on p.BrandId equals b.BrandId
                       select new
                                  {
                                      Subcategory = s.SubCatName,
                                      Brand = b.BrandName,
                                      p.ProductName,
                                      p.ProductPrice
                                  }).Where(x => x.Subcategory == subcategory);


        foreach (var product in products)
        {
            productList.Add(product);
        }

        foreach (var produs in productList){
            Console.WriteLine(produs.ProductName);
        }

When I try to do this I get an error that says:

Object does not contain a definion for ProductName

The same goes for all the other fields two

Aldo if I try and do this:

Console.WriteLine(produs);

I get tables with the data for each field

I have run this for tests on LINQPAD and it also does not work in visual studio.What am I doing wrong?

  • 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-16T10:20:20+00:00Added an answer on June 16, 2026 at 10:20 am

    This is the problem:

    List<Object> productList = new List<Object>();
    

    You’re declaring it as a List<object>, which means when you later write this:

    foreach (var produs in productList)
    

    Then produs is implicitly typed as object.

    The simplest approach would be just to use ToList() instead of copying the results to a list yourself:

    var products = (from p in Products
                    join s in SubCategories.Where(x => x.SubCatName == subcategory)
                      on p.SubcatId equals s.SubCatId
                    join b in Brands on p.BrandId equals b.BrandId
                    select new {
                        Subcategory = s.SubCatName,
                        Brand = b.BrandName,
                        p.ProductName,
                        p.ProductPrice
                    }).ToList();
    

    Note that I’ve moved the subcategory name filter as early as possible – there’s no need to do it after the join. The ToList() call at the end will mean the result is a List<T> where T is your anonymous type.

    Then you can use:

    foreach (var product in products)
    {
        Console.WriteLine(produs.ProductName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the query I'm trying to convert into Linq: SELECT R.Code, R.FlightNumber, S.[Date], S.Station,
So I was trying to convert a SQL into LINQ query the logic is:
I am trying to convert this sql into my LINQ query and am having
im trying to convert my result from a linq query to json using json.net
I'm trying to convert this SQL query to LINQ: SELECT Items.p_Name Product, DiamondCategory.dc_Name Category,
I am trying to convert the following SQL into a LINQ expression SELECT COUNT(ID)
I am trying to convert a SQL query to LINQ. Somehow my count(distinct(x)) logic
I have the SQL and trying to convert ti to Linq query. I need
I'm trying to convert an SQL query to Linq and I get an exception:
Im trying to convert the output of a LINQ query to a datatable, I

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.