2个复杂查询的{C#Linq UNION语法

时间:2017-05-05 20:02:46

标签: linq group-by aggregate union-all

对于刚刚学习Linq的人来说,这非常复杂!我询问了如何在this question中加入和GROUP BY以及 BIG SHOUT OUT TriV,以获得明确且非常有用的答案。

现在,我想采用我已经非常复杂的查询并为类似的查询创建一个UNION。我在下面有两个单独的 SQL查询,我使用UNION子句将其放入1个查询中。

第一个查询连接两个表。但是第二个查询仅针对一个表,因为它适用于未链接到组织表中的组织的未分配资产。第一个查询将为每个组织返回一个单独的行。对于所有未分配的资产,第二个查询将只返回一行。

整个SQL查询如下所示:

SELECT  o.org_hq_name,
        o.org_command_name,
        o.org_region_name,
        o.org_installation_name,
        o.org_site_name,
        o.org_subsite_name,
        o.org_hq_id,
        o.org_command_id,
        o.org_region_id,
        o.org_installation_id,
        o.org_site_id,
        count(org_site_id) AS count
FROM    organization o, asset a
WHERE   o.org_hq_id = a.hq_org_id
AND     o.org_command_id = a.command_org_id
AND     o.org_region_id = a.region_org_id
AND     o.org_installation_id = a.installation_org_id
AND     o.org_site_id = a.site_org_id
GROUP BY o.org_hq_name,
        o.org_command_name,
        o.org_region_name,
        o.org_installation_name,
        o.org_site_name,
        o.org_subsite_name,
        o.org_hq_id,
        o.org_command_id,
        o.org_region_id,
        o.org_installation_id,
        o.org_site_id

UNION ALL

SELECT  'Unassigned',
        'Unassigned',
        'Unassigned',
        'Unassigned',
        'Unassigned',
        a.hq_org_id,
        a.command_org_id,
        a.region_org_id,
        a.installation_org_id,
        a.site_org_id,
        count(org_site_id) AS count
FROM    asset a
WHERE   a.unassigned = 1
GROUP BY a.hq_org_id,
        a.command_org_id,
        a.region_org_id,
        a.installation_org_id,
        a.site_org_id

我有以下两个单独的 Linq查询,我想使用UNION子句将其放入1 Linq查询中。我相信下面的代码只需要一点点推文即可实现。我尝试过使用其他问题的例子,但我仍然无法解决这个问题。

var join1 =  from a in context.asset
         join o in context.organization
         on new {hqID = a.hq_org_id, commandID = a.command_org_id, regionID = a.region_org_id, installationID = a.installation_org_id, siteID = a.site_org_id}
         equals new {hqID = o.hq_id, commandID = o.command_id, regionID = o.region_id, installationID = o.installation_id, siteID = o.site_id}
         group new {a,o} by new {   o.org_hq_name,
                                    o.org_command_name,
                                    o.org_region_name,
                                    o.org_installation_name,
                                    o.org_site_name,
                                    o.org_hq_id,
                                    o.org_command_id,
                                    o.org_region_id,
                                    o.org_installation_id,
                                    o.org_site_id
                                } into gr1
         select new
         {
            org_hq_name = gr1.Key.org_hq_name,
            org_command_name = gr1.Key.org_command_name,
            org_region_name = gr1.Key.org_region_name,
            org_installation_name = gr1.Key.org_installation_name,
            org_site_name = gr1.Key.org_site_name,
            org_hq_id = gr1.Key.org_hq_id,
            org_command_id = gr1.Key.org_command_id,
            org_region_id = gr1.Key.org_region_id,
            org_installation_id = gr1.Key.org_installation_id,
            org_site_id = gr1.Key.org_site_id,
            Count = gr1.Count()
         };


UNION


var join2 =  from a in context.asset
         where (a.hq_org_id == 0)
         group m by new {  a.hq_org_id,
                           a.command_org_id,
                           a.region_org_id,
                           a.installation_org_id,
                           a.site_org_id
                        } into gr2
         select new
         {
            org_hq_name = "unknown",
            org_command_name = "unknown",
            org_region_name = "unknown",
            org_installation_name = "unknown",
            org_site_name = "unknown",
            org_hq_id = gr2.Key.org_hq_id,
            org_command_id = gr2.Key.org_command_id,
            org_region_id = gr2.Key.org_region_id,
            org_installation_id = gr2.Key.org_installation_id,
            org_site_id = gr2.Key.org_site_id,
            Count = gr2.Count()
         };

提前感谢您的帮助。我相信答案对于那些正在努力解决这类复杂查询的人来说会有所帮助。

1 个答案:

答案 0 :(得分:-1)



    class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

// This method creates and returns an array of Pet objects.
static Pet[] GetCats()
{
    Pet[] cats = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };
    return cats;
}

// This method creates and returns an array of Pet objects.
static Pet[] GetDogs()
{
    Pet[] dogs = { new Pet { Name="Bounder", Age=3 },
                   new Pet { Name="Snoopy", Age=14 },
                   new Pet { Name="Fido", Age=9 } };
    return dogs;
}

public static void ConcatEx1()
{
    Pet[] cats = GetCats();
    Pet[] dogs = GetDogs();

    // Concatenate a collection of cat names to a
    // collection of dog names by using Concat().
    IEnumerable<string> query =
        cats.AsQueryable()
        .Select(cat => cat.Name)
        .Concat(dogs.Select(dog => dog.Name));

    foreach (string name in query)
        Console.WriteLine(name);
}

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido
&#13;
&#13;
&#13;