Solved

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

Posted on 2011-03-09
6
542 Views
Last Modified: 2013-11-11
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
Comment
Question by:KMBoyer
  • 3
  • 3
6 Comments
 
LVL 21

Expert Comment

by:naspinski
Comment Utility
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
 

Author Comment

by:KMBoyer
Comment Utility
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
 
LVL 21

Expert Comment

by:naspinski
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:KMBoyer
Comment Utility
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
 
LVL 21

Accepted Solution

by:
naspinski earned 500 total points
Comment Utility
if (chkWestern.Checked) regions.Add("Western");(

Open in new window

and so on would be the simplest way
0
 

Author Closing Comment

by:KMBoyer
Comment Utility
Thank you very much.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now