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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:20:49+00:00 2026-06-04T10:20:49+00:00

In T-SQL you can use CROSS APPLY to get all possible variations between the

  • 0

In T-SQL you can use CROSS APPLY to get all possible variations between the table left and right from the statement. Now I have the following situation in C# and I hope there is a way to solve my problem using LINQ-to-Objects.

I have a list with TestData objects (like below) which is similar to the KeyValuePair<string, object> object (Just a Key and a Value property):
The key can be everything and there can be multiple objects with the same key.

IList<KeyValuePair<String, Object>> objects;
// Content of list
// # | Key  | Value
// 1 | "A"  | 1
// 2 | "A"  | 2
// 3 | "A"  | 3
// 4 | "B"  | 4
// 5 | "B"  | 5
// 6 | "C"  | 6
// 7 | "D"  | 7
// 8 | "D"  | 8

I have also a list of requested keys:

IList<String> requestedKeys = new List<string>() { "A", "D" };

Now I want to have all possible combinations of KeyValuePair objects between the keys in the requestedKeys list.

IList<IList<KeyValuePair<String, Object>>> result = ...
// Content of 'result' will be in this example 6 lists with each 2 KeyValuePair objects
// #  | "A" | "D" | (If there are more in the requestedKey list then there are more KeyValuePair items in the innerlist.)
// 1  |  1  |  7  |
// 2  |  2  |  7  |
// 3  |  3  |  7  |
// 4  |  1  |  8  |
// 5  |  2  |  8  |
// 6  |  3  |  8  |

Is it possible to solve my problem using LINQ-to-Objects. If not can you tell me the most efficient way to build it anyway.


EDIT 1:
To make more clear what the result should be:
I want to have a LINQ-to-Objects query something like this:
@Joanna thanks for the tip about multiple froms but the problem is: With this syntax you cannot have a dynamic amount of froms. In my case I need as many froms as items in the requestedKeys list

var result =    
   from listA in objects.Where(m => m.Key == "A")
   from listD in objects.Where(m => m.Key == "D")
   // from .....
   // from .....
   // overhere as many froms as items in 'requestedKeys' list   
select new [] { listA, listD /*, All other lists */ }
  • 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-04T10:20:51+00:00Added an answer on June 4, 2026 at 10:20 am

    I found the solution myself:

    It is a very complex join in LINQ because each item in the requestKeys list requires an extra cross join. Regarding to the given example list, the result should be objects.Count(m => m.Key == "A") * objects.Count(m => m.Key == "D") (result is 3 * 2 = 6). Each extra item in the list causes an extra multiply of the whole result set.

    So this is the result:

    // The result list
    IEnumerable<IList<KeyValuePair<char, int>>> result;
    
    // If there are no requestedKeys there is no result expected
    if(requestedKeys.Count() > 0)
    {
        // Loop through all request keys to cross join them together
        foreach (var key in requestedKeys)
        {
            if (result == null)
            {
                // First time the innerlist List<KeyValuePair<char, int>> will contain 1 item
                // Don't forget to use ToList() otherwise the expression will be executed to late.
                result = objects.Where(m => m.Key == key).Select(m => new List<KeyValuePair<char, int>>() { m }).ToList();
            }
            else
            {
                // Except for the first time the next subresult will be cross joined
                var subresult = objects.Where(m => m.Key == key).Select(m => new List<KeyValuePair<char, int>>() { m });
                result = result.Join(
                    subresult,
                    l1 => 0, // This and the next parameter does the cross join trick
                    l2 => 0, // This and the previous parameter does the cross join trick
                    (l1, l2) => l1.Concat(l2).ToList() // Concat both lists which causes previous list plus one new added item
                    ).ToList(); // Again don't forget to 'materialize' (I don't know it is called materialization in LINQ-to-Objects 
                                // but it has simular behaviors because the expression needs to be executed right away)
            }
        }           
    }
    return result;
    

    Unfortunately it is not completely LINQ so if someone know an better solution. Please comment me or answer my question 🙂

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

Sidebar

Related Questions

How can I use something equivalent of CROSS APPLY in SQL Server 2000 ?
In SQL you can use SELECT * FROM INFORMATION_SCHEMA.TABLES etc to get information about
Anyone have a PL-SQL statement that i can use to generate database & tables
It's clear that you can use numeric characters in SQL table names and use
I'm trying to use CROSS APPLY in SQL, but only want to use the
In SQL Server you can use an XML datatype and map it to relational
In Oracle / SQL Server I can use the WITH keyword to define a
I'm wondering if I can use SQL Server CE instead of SQL Server Express+
I can use the sql server management studio to open a sqlserver 2000 database,
You can use the Filter property of a BindingSource to do SQL like filtering.

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.