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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:44:49+00:00 2026-05-17T14:44:49+00:00

I have a fairly complicated linq query that returns an item and a string[]

  • 0

I have a fairly complicated linq query that returns an item and a string[] of one column in the item’s many-to-many relationship.

So I return an anonymous type sort of like this:

var q = from k in db.Items
        join m in db.ManyRel on k.Id equals m.ItemID
        select new
        {
            k,
            list = (from t in m
                    where t.ItemID == k.ID
                    select t.Name)
        }

So the anonymous type is {IQueryable<k>, IQueryable<string>}.

What I then want to do is search through the list for intersections from string[] input.

q.Select(x => new 
                 { 
                    x.k, 
                    PartialMatches = ?????, 
                    ExactMatches = x.tags.Intersect(input).Count()
                 });

I currently have it working with .Intersect, but .Intersect() only matches against the entire element. I would like to somehow use .Contains() within .Intersect() to count the number of partial matches.

I would prefer to do this without an extension method if possible.

Edit: @Jon & Timwi

I tried both of your versions, they were pretty much the same, and they find if ANY item in input partially exists within x.tags. I’d like to be able to count how many items in input partially exist within x.tags, not just whether AN item exists. Thanks for the responses.

I think I have a better handle on the AsEnumerable() stuff than I did a few nights ago.

Also, for the record, because you where both curious, the Intersect() method does not translate to Linq to SQL and neither do the any methods you each proposed. They all work well and fast enough with AsEnumberable() however.

  • 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-17T14:44:49+00:00Added an answer on May 17, 2026 at 2:44 pm

    You don’t need to use Intersect, and it doesn’t need to have a translation to SQL. You can do it in C#:

    var results = q.AsEnumerable().Select(x => new 
                  { 
                     x.k, 
                     PartialMatches = x.tags.Count(tag => input.Any(
                                         word => word.Contains(tag))),
                     ExactMatches = x.tags.Intersect(input).Count()
                  });
    

    The call to AsEnumerable() detaches this part of the query from the database. Everything before it will be retrieved from the database, and the rest will be processed in C#.

    You could, in theory, do the ExactMatches in SQL (if LINQ to SQL can translate it to SQL, which I’m not sure it can), but it is not obvious whether that is more efficient that doing it in C#. It depends on the relative sizes of the collections involved, including input.

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

Sidebar

Related Questions

I have a SQL query that's fairly complicated and involves multiple subqueries and unions.
I have a fairly complicated join query that I use with my database. Upon
I have a fairly complicated installer that I'm writing in Wix that has a
Just out of curiosity, for applications that have a fairly complicated module tree, would
I have a project that has a fairly complicated, nested (references calling the referenced
I have a build process that creates an ear in a fairly complicated manner
I have a fairly complicated build workflow for one of my Jenkins builds. The
This one is killing me. I have a fairly complicated JavaScript routine fired by
I have an object under test that makes a fairly complicated call to a
I have a fairly complicated SQL query with a nested subquery. When I try

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.