LINQ series: LINQ to SQL concat/union

Source: Internet
Author: User

1. Concat

  Single row concat

 var  expr = (from  P in   context.            Products  select   P.productname) . Concat ( from  C in   context.            Categories  select   C.categoryname ); foreach  (var  item in   expr) {Console.WriteLine (item);}  
var expr = context. Products    = p.productname)    . Concat (    context. Categories    = c.categoryname)    );
SELECT     [UnionAll1].[ProductName]  as [C1]     from(SELECT         [Extent1].[ProductName]  as [ProductName]         from [dbo].[Product]  as [Extent1]    UNION  All        SELECT         [Extent2].[CategoryName]  as [CategoryName]         from [dbo].[Category]  as [Extent2]) as [UnionAll1]

Multi-column Concat

var expr = context. Products    new     {        p.productid,         p.productname     })    . Concat (    context. Categories    new     {         = C.categoryid,         = c.categoryname     })    );
SELECT     [UnionAll1].[ProductID]  as [C1],     [UnionAll1].[ProductID1]  as [C2],     [UnionAll1].[ProductName]  as [C3]     from(SELECT         [Extent1].[ProductID]  as [ProductID],         [Extent1].[ProductID]  as [ProductID1],         [Extent1].[ProductName]  as [ProductName]         from [dbo].[Product]  as [Extent1]    UNION  All        SELECT         [Extent2].[CategoryID]  as [CategoryID],         [Extent2].[CategoryID]  as [CategoryID1],         [Extent2].[CategoryName]  as [CategoryName]         from [dbo].[Category]  as [Extent2]) as [UnionAll1]

2. Union

var expr = context. Products    = p.productname)    . Union (    context. Categories    = c.categoryname)    );
SELECT     [Distinct1].[C1]  as [C1]     from(SELECT DISTINCT         [UnionAll1].[ProductName]  as [C1]         from(SELECT             [Extent1].[ProductName]  as [ProductName]             from [dbo].[Product]  as [Extent1]        UNION  All            SELECT             [Extent2].[CategoryName]  as [CategoryName]             from [dbo].[Category]  as [Extent2]) as [UnionAll1]    )   as [Distinct1]

LINQ series: LINQ to SQL concat/union

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.