zoukankan      html  css  js  c++  java
  • LINQ系列:LINQ to SQL Concat/Union

    1. Concat

      单列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
        .Select(p => p.ProductName)
        .Concat(
        context.Categories
        .Select(c => 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]

      多列Concat

    var expr = context.Products
        .Select(p => new 
        {
            p.ProductID, 
            p.ProductName 
        })
        .Concat(
        context.Categories
        .Select(c => new 
        { 
            ProductID = c.CategoryID, 
            ProductName = 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
        .Select(p => p.ProductName)
        .Union(
        context.Categories
        .Select(c => 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]
  • 相关阅读:
    SAP 标准成本滚算小记
    记一次SAP新业务开发项目
    让人头疼的关键用户
    SAP GUI个性化设置
    惊心动魄的SAP S4客户额度调整运动
    最新.net和Java调用SAP RFC中间件下载
    那些年我遇到的ERP顾问
    【SAP业务模式】之STO(二):系统配置
    SAP S4系统创建Customer和Vendor的BAPI
    【SAP S/4 1511之变】:主数据之变
  • 原文地址:https://www.cnblogs.com/libingql/p/4050761.html
Copyright © 2011-2022 走看看