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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:39:23+00:00 2026-05-13T17:39:23+00:00

I’m fairly new to Linq and struggling using dynamic where over a many to

  • 0

I’m fairly new to Linq and struggling using dynamic where over a many to many relationship.

Database tables are like so:

Products <-> Products_SubCategories <-> SubCategories

with Products_SubCategories being a link table.

My full linq statement is

 db.Products.Where("it.SubCategories.SubCategoryID = 2")
                   .Include("SubCategories")
                   .OrderBy(searchOrderBy)
                   .Skip(currentPage * pageSize)
                   .Take(pageSize)
                   .ToList()
                   .ForEach(p => AddResultItem(items, p));

So ignoring everything bar the Where() I’m just trying to pull out all products which are linked to sub category ID 2, this fails with

To extract properties out of collections, you must use a sub-query to iterate over the collection., near multipart identifier, line 8, column 1.

I think using the SQL-esque syntax I can do a subquery as per this link. However I’m not sure how to do that in the lambda / chaining syntax.

This is the start of a search function and I would like to build up the where string dynamically, as I have with the searchOrderBy string to avoid a large SELECT CASE. Products is linked to another table via a link table that I will need to include once I understand how to do this example.

Any help would be much appreciated!

Thanks

  • 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-13T17:39:23+00:00Added an answer on May 13, 2026 at 5:39 pm

    This is wrong:

    db.Products.Where("it.SubCategories.SubCategoryID = 2")
    

    SubCategories is a list. It does not have a property called SubCategoryID. Rather, it contains a group of entities which each have a property called SubCategoryID. That’s a critical distinction.

    When you run into a situation where you don’t know how to proceed in there are multiple problems, it is good to break the problem down into several, smaller problems.

    Let’s start by removing the dynamic query. It will be easier to solve the problem with a non-dynamic query. Once you’ve done that, you can go back and make it dynamic again.

    So start by using the non-dynamic syntax. Type something like this in Visual Studio, and see what IntelliSense does for you:

    db.Products.Where(p => p.SubCategories.
    

    You will quickly see that there is no SubCategoryID property. Instead, you will see a bunch of LINQ API methods for working with lists. If you know LINQ well, you will recognize that the Any() method is what you want here:

    db.Products.Where(p => p.SubCategories.Any(sc => sc.SubCategoryID == 2))
    

    Go ahead and run that query. Does it work? If so, you can move ahead to making it dynamic. I’m no ESQL expert, but I’d start with something along the lines of:

    db.Products.Where("EXISTS(SELECT SC FROM it.SubCategories AS SC WHERE SC.SubCategoryID = 2");
    

    As an aside, I use MS Dynamic Query (“Dynamic LINQ”) for this sort of thing rather than Query Builder, as it’s more testable.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported

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.