Solved

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

Posted on 2011-03-09
6
551 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 500 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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