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

The Archive Base Latest Questions

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

Im using Linq to return IDs from 4 cascading dropdown menus. The user may

  • 0

Im using Linq to return IDs from 4 cascading dropdown menus.

The user may have selected 1 or more values from either 1 or all of the menus. From the users selections, Im then quering the text columns of a DataTable for the representative IDs.

If the user selects from the 4th (and lowest level) dropdown, then they would have selected from all those above.

If the user selects “X” from menu 1, “Y” from menu 2, and nothing in the others i would expect to see results1 with say 10 rows where “X” exists in [Col_1], which would then be queried down to say 5 rows in results2 where “Y” exists in [Col_2].

EDIT
The Code (in a basic form)

var results1 = 
    from a in dt.AsEnumerable()
    where stringArray1.Contains([Col1])
    select a;


var results2 = 
    from a in results1
    where stringArray2.Contains([Col2])
    select a;

var results3 = 
    from a in results2
    where stringArray3.Contains([Col3])
    select a;

var results4 = 
    from a in results3
    where stringArray4.Contains([Col4])
    select a;

var results = results4 ?? results3 ?? results2 ?? results1;

results4 depends on the results from results3, results3 on results2, and results2 on results1.

It may be that results4 - 2 are empty , and therefore I was wondering if I could coalesce these into a final variable, eg:

var results = results4 ?? results3 ?? results2 ?? results1;

This is throwing the error:

Operator '??' cannot be applied to operands of type 'System.Collections.Generic.IEnumerable<AnonymousType#4>'

Does anyone have any cunning ideas how to resolve this? Im trying not to write very long winded code, handling every possible outcome.

Thank you all in advance (hopefully the question makes sense!)

CM

  • 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-04T17:49:26+00:00Added an answer on June 4, 2026 at 5:49 pm

    So based on the queries you’re using, none of the results will ever be null. Some of them may be empty, but they won’t be null. This means that the ?? operator won’t ever do anything useful.

    How do you want this to behave? Do you want to say, “if this result is empty, use this other one?” For that use the method I’ve written below.

    public static IEnumerable<T> Coalesce<T>(IEnumerable<T> first, IEnumerable<T> second)
    {
      if(first == null || !first.Any())
      {
        return second;
      }
      else
      {
        return first;
      }
    }
    

    This could then be used as:

    Coalesce(results4, Coalesce(results3, Coalesce(results2, results1)));
    

    The result of this is, “return result4, unless it’s empty. If it’s empty return results3. If results3 is empty return results2, if results2 is empty return results1.”

    You could make it an extension method if you think you’d use it enough. It would make it prettier, but would also clutter up intellisense for all of the other IEnumerables so it may not be worth it.

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

Sidebar

Related Questions

I'm trying to return a set of distinct values from a DataTable using LINQ.
How can I return first 100 records using Linq? I have a table with
I am using dynamic Linq to return data for user-input search criteria. My query
Can I using LINQ return a List<Person> from a List<String> like the following: //
What should i return from a WCF service when using LINQ? e.g.: var res
I am trying to return a binary from the DB using linq for display
I have a list using this Linq query filterEntities = (from list in filterEntities
Is it possible to return a single value and an enumerable collection using LINQ
Using LINQ to Entities, how can I determine if any item from a List
Using LINQ can I get the data from the table into the format shown

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.