[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

How can I concatenate the results of two or more Linq querys into on result set.

How can I concatenate the results of two or more Linq querys into on result set.

Just as an example lets say that I have:

        if (chkWestern.Checked)
        {
            var c1 = from c in db.vw_ChapterWithRegionNames
                     where c.RegionName == "Western"
                     select c;
        }

        if (chkNorthwest.Checked)
        {
            var c2 = from c in db.vw_ChapterWithRegionNames
                     where c.RegionName == "Northwest"
                     select c;
        }

And I want to concatenate the results into one result set to bind to a ListView or Gridview.
Like
lv.DataSource = ????;
lv.DataBind();

Thanks in advance.
0
KMBoyer
Asked:
KMBoyer
  • 3
  • 3
1 Solution
 
naspinskiCommented:
First of all, you will have to declare c1 and c2 outside of the if statements (most likely IEnumerable<vw_ChapterWithRegionName>) -
Then you can use
var c3 = c1.Union(c2);

Open in new window

but they must always be of the same type;
0
 
KMBoyerAuthor Commented:
Actually It dawns on me that I am going about this the hard way. My situation is that I have 8 check boxes that represent the different regions. All could be checked or none or some. I want the output in one list view. Is there a simpler more direct way to handle this rather than what I was proposing?

Thanks
0
 
naspinskiCommented:
try this:
List<string> regions = new List<string>();
//add in all the strings that you want included from checkboxes
var query = db.vw_ChapterWithRegionNames.Where(c => regions.Contains(c.RegionName));

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
KMBoyerAuthor Commented:
It must be my stupid day, but can you show me exactly how I would "add in all the strings that you want included from checkboxes".
0
 
naspinskiCommented:
if (chkWestern.Checked) regions.Add("Western");(

Open in new window

and so on would be the simplest way
0
 
KMBoyerAuthor Commented:
Thank you very much.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now