Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ - using union

Posted on 2015-02-02
12
Medium Priority
?
106 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
  • 6
  • 5
12 Comments
 
LVL 64

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1000 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 64

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 64

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 1000 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 64

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 64

Expert Comment

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

972 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