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