Solved

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

Posted on 2011-03-09
6
546 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

23 Experts available now in Live!

Get 1:1 Help Now