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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:53:43+00:00 2026-05-11T02:53:43+00:00

Here’s an example of the query I’m trying to convert to LINQ: SELECT *

  • 0

Here’s an example of the query I’m trying to convert to LINQ:

SELECT * FROM Users WHERE Users.lastname LIKE '%fra%'     AND Users.Id IN (          SELECT UserId           FROM CompanyRolesToUsers           WHERE CompanyRoleId in (2,3,4) ) 

There is a FK relationship between CompanyRolesToUsers and Users, but it’s a many to many relationship and CompanyRolesToUsers is the junction table.

We already have most of our site built, and we already have most of the filtering working by building Expressions using a PredicateExtensions class.

The code for the straightforward filters looks something like this:

 if (!string.IsNullOrEmpty(TextBoxLastName.Text))  {      predicateAnd = predicateAnd.And(c => c.LastName.Contains(                                      TextBoxLastName.Text.Trim()));  }  e.Result = context.Users.Where(predicateAnd); 

I’m trying to add a predicate for a subselect in another table. (CompanyRolesToUsers)

What I’d like to be able to add is something that does this:

int[] selectedRoles = GetSelectedRoles(); if( selectedRoles.Length > 0 ) {     //somehow only select the userid from here ???:     var subquery = from u in CompanyRolesToUsers                    where u.RoleID in selectedRoles                    select u.UserId;      //somehow transform this into an Expression ???:     var subExpression = Expression.Invoke(subquery);      //and add it on to the existing expressions ???:     predicateAnd = predicateAnd.And(subExpression); } 

Is there any way to do this? It’s frustrating because I can write the stored procedure easily, but I’m new to this LINQ thing and I have a deadline. I haven’t been able to find an example that matches up, but I’m sure it’s there somewhere.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:53:44+00:00Added an answer on May 11, 2026 at 2:53 am

    Here’s a subquery for you!

    List<int> IdsToFind = new List<int>() {2, 3, 4};  db.Users .Where(u => SqlMethods.Like(u.LastName, '%fra%')) .Where(u =>     db.CompanyRolesToUsers     .Where(crtu => IdsToFind.Contains(crtu.CompanyRoleId))     .Select(crtu =>  crtu.UserId)     .Contains(u.Id) ) 

    Regarding this portion of the question:

    predicateAnd = predicateAnd.And(c => c.LastName.Contains(                                 TextBoxLastName.Text.Trim())); 

    I strongly recommend extracting the string from the textbox before authoring the query.

    string searchString = TextBoxLastName.Text.Trim(); predicateAnd = predicateAnd.And(c => c.LastName.Contains( searchString)); 

    You want to maintain good control over what gets sent to the database. In the original code, one possible reading is that an untrimmed string gets sent into the database for trimming – which is not good work for the database to be doing.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I would use a label and an button to show… May 11, 2026 at 12:35 pm
  • added an answer Visual Studio Project implementations provide a list of files that… May 11, 2026 at 12:35 pm
  • added an answer It's a bug within 1.3.2. I don't see the behavior… May 11, 2026 at 12:35 pm

Related Questions

Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.