?
Solved

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

Posted on 2011-03-09
6
Medium Priority
?
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 21

Expert Comment

by:naspinski
ID: 35085618
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
ID: 35085930
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
ID: 35085996
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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 

Author Comment

by:KMBoyer
ID: 35086187
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 2000 total points
ID: 35086211
if (chkWestern.Checked) regions.Add("Western");(

Open in new window

and so on would be the simplest way
0
 

Author Closing Comment

by:KMBoyer
ID: 35086825
Thank you very much.
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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