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 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

Related Questions

Here is my query: select word_id, count(sentence_id) from sentence_word group by word_id having count(sentence_id)
Here is the code in a function I'm trying to revise. This example works
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here I am trying to retrieve the response from the server and display it,
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
Here is my error(if you need any more info just ask)- Error SQL query:
Here is the field declaration in a form: max_number = forms.ChoiceField(widget = forms.Select(), choices
Here is an object that I'd like to use with ng-repeat, but it's not

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.