Solved

LINQ - using union

Posted on 2015-02-02
12
100 Views
Last Modified: 2015-02-17
I have the following Transact SQL query using a union. I need some samples as to how this would look in LINQ.
 internaltional_zip does not have id.
 
 
SELECT transnoid, zip from domestic_zip
UNION
SELECT '' as transnoid, zip from internaltional_zip
0
Comment
Question by:VBdotnet2005
[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
  • 6
  • 5
12 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40584856
Hi VBdotnet2005;

Have you tried something like this?

Dim result = (From CS In ctx.tbl_zipcode_assignment
              Select CS.Zip).Distinct()

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 40584867
Hi Fernando,

I changed my question a few minutes ago.
0
 

Author Comment

by:VBdotnet2005
ID: 40584869
I would like to know on how to do the following in LINQ instead.
SELECT transnoid, zip from domestic_zip
UNION
SELECT '' as transnoid, zip from internaltional_zip
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 40584988
Hi VBdotnet2005;

OK then, this will work as long as in both the select clause the order of the fields name are in the same order and data type.

Dim results = (From z In Context.domestic_zip _
               Select z.transnoid, z.zip) _
              .Union( _
              (From z In Context.internaltional_zip _
               Select z.transnoid, z.zip))

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40584991
Did you also change the topic areas?
0
 

Author Comment

by:VBdotnet2005
ID: 40585178
My problem is there is no transnoid in internaltional_zip .
0
 

Author Comment

by:VBdotnet2005
ID: 40585179
What would you suggest to do?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40585297
To your statements, "My problem is there is no transnoid in internaltional_zip ." and "What would you suggest to do?", well I can only go by what you stated and in your SQL statement, you state that you have two columns in both tables you want to return, one being  transnoid and the other being zip. If you are stating that you do not have transnoid how can I suggest something if I do not know what is available or what your requirements are.
0
 
LVL 15

Assisted Solution

by:Lokesh B R
Lokesh B R earned 250 total points
ID: 40585806
Hi,

Please find the sample code in LINQ for UNION

        public class DomesticZip
        {
            public int? transnoid { get; set; }
            public int Zip { get; set; }
        }

List<DomesticZip> domestic = new List<DomesticZip>();
            domestic.Add(new DomesticZip() { transnoid = 1, Zip = 50001 });
            domestic.Add(new DomesticZip() { transnoid = 2, Zip = 50002 });
            domestic.Add(new DomesticZip() { transnoid = 3, Zip = 50003 });
            domestic.Add(new DomesticZip() { transnoid = 4, Zip = 50004 });

            List<DomesticZip> international = new List<DomesticZip>();
            international.Add(new DomesticZip() { Zip = 60001 });
            international.Add(new DomesticZip() { Zip = 60002 });
            international.Add(new DomesticZip() { Zip = 60003 });
            international.Add(new DomesticZip() { Zip = 60004 });

            var query = domestic.Select(c => new DomesticZip { transnoid = c.transnoid, Zip = c.Zip });

            var query1 = international.Select(c => new DomesticZip { transnoid = c.transnoid, Zip = c.Zip });

           var output = query.Union(query1);

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40585903
@Lokesh B R;

The authors resent statement is that transonic is not a member of the table.
0
 

Author Comment

by:VBdotnet2005
ID: 40601781
thank you
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40602190
Was your question answered?
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

622 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