C# PostgreSQL是否可以使用WHERE by某些IF条件?

C# PostgreSQL是否可以使用WHERE by某些IF条件?,c#,postgresql,.net-core,npgsql,C#,Postgresql,.net Core,Npgsql,我对报告有以下SQL请求 select customers."AppId", second_dep "SecondDeps", first_dep "FirstDeps", customers_count "Customers", registrations "Registrations" From (select Count("AppId") as customers_count, "AppId" FROM "Customers" joi

我对报告有以下SQL请求

select customers."AppId", second_dep "SecondDeps", first_dep "FirstDeps",
       customers_count "Customers", registrations "Registrations"
From (select Count("AppId") as customers_count, "AppId"
      FROM "Customers"
               join "Advertisers" A on "Customers"."AdvertiserId" = A."AdvertiserId"
               join "Categories" C2 on "Customers"."CategoryId" = C2."CategoryId"

      where A."Name" in (:AdvertiserNames)
        AND C2."Name" = :CategoryName
      GROUP BY "AppId"
     ) as customers

         left join

     (select C."AppId", count(CE.*) as second_dep
      from "CustomerEvents" as CE
               inner join "Customers" C on CE."CustomerId" = C."CustomerId"
      WHERE "EventType" = 'deposit'
        and "Again" = TRUE
      GROUP BY C."AppId") as dep2 on customers."AppId" = dep2."AppId"

        left  join

     (select C."AppId", count(CE.*) as first_dep
      from "CustomerEvents" as CE
               inner join "Customers" C on CE."CustomerId" = C."CustomerId"
      WHERE "EventType" = 'deposit'
        and "Again" = false
      GROUP BY C."AppId") as dep on customers."AppId" = dep."AppId"

         left join

     (select C."AppId", count(CE.*) as registrations
      from "CustomerEvents" as CE
               inner join "Customers" C on CE."CustomerId" = C."CustomerId"
      WHERE "EventType" = 'registration'
      GROUP BY C."AppId") as regs on regs."AppId" = customers."AppId";
有问题的字符串是

where A."Name" in (:AdvertiserNames)
如果广告客户名称为空,我想跳过它。可能吗?好的,我可以在代码端检查它,但是这种方式会导致我复制整个请求,但有一些小的区别,我的意思是,如果广告客户名称为空,则运行SQL,而不在其中运行A.Name:广告客户名称。或者我可以使用连接来获得合适的SQL。我也不喜欢这种方式

关于我的技术堆栈。它是带有PostgreSQL的.NETCore2.2。以下是整个报告方法的代码:

public IQueryable<ByApplicationsReportModel> ByApplications(string category, List<string> advertisers)
{
    var rawSql = new RawSqlString(@"
        select customers.""AppId"", second_dep ""SecondDeps"", first_dep ""FirstDeps"",
    customers_count ""Customers"", registrations ""Registrations""
            From (select Count(""AppId"") as customers_count, ""AppId""
            FROM ""Customers""
            join ""Advertisers"" A on ""Customers"".""AdvertiserId"" = A.""AdvertiserId""
            join ""Categories"" C2 on ""Customers"".""CategoryId"" = C2.""CategoryId""
            where A.""Name"" in (@AdvertiserNames)
            AND C2.""Name"" = @CategoryName
            GROUP BY ""AppId""
                ) as customers

                left join

                (select C.""AppId"", count(CE.*) as second_dep
                from ""CustomerEvents"" as CE
            inner join ""Customers"" C on CE.""CustomerId"" = C.""CustomerId""
            WHERE ""EventType"" = 'deposit'
            and ""Again"" = TRUE
            GROUP BY C.""AppId"") as dep2 on customers.""AppId"" = dep2.""AppId""

            left  join

                (select C.""AppId"", count(CE.*) as first_dep
                from ""CustomerEvents"" as CE
            inner join ""Customers"" C on CE.""CustomerId"" = C.""CustomerId""
            WHERE ""EventType"" = 'deposit'
            and ""Again"" = false
            GROUP BY C.""AppId"") as dep on customers.""AppId"" = dep.""AppId""

            left join

                (select C.""AppId"", count(CE.*) as registrations
                from ""CustomerEvents"" as CE
            inner join ""Customers"" C on CE.""CustomerId"" = C.""CustomerId""
            WHERE ""EventType"" = 'registration'
            GROUP BY C.""AppId"") as regs on regs.""AppId"" = customers.""AppId""");

    var advertisersParam = new NpgsqlParameter("AdvertiserNames",  
        string.Join(",", advertisers) );

    var categoryParam = new NpgsqlParameter("CategoryName", category);

    return _context.ByApplicationsReportModels
        .FromSql(rawSql, categoryParam, advertisersParam);
}

有什么想法吗

您可以尝试将@AdvertiserNames中的where A.Name更改为@AdvertiserNames或@AdvertiserNames=中的where A.Name。

您可以直接将字符串数组传递给查询,而不是将广告商连接成字符串:

var advertisersParam=新的NpgsqlParameterAdvertiserNames,广告商; 在SQL中,您需要更改为x=ANY@advisors,而不是在@advisors构造中使用x


注意:如果您想在@广告商为空时通过检查,您还需要一个附加条款。

谢谢!但我如何检查@广告商是否为空?我是说广告商在哪里。名称=ANY@AdvertiserNames或者?您可以通过检查数组的长度来检查数组是否为空,即。cardinality@advertisers > 0