Untitled

mail@pastecode.io avatar
unknown
csharp
25 days ago
2.1 kB
3
Indexable
Never
public async Task<List<string>?> GetSubnetIps(string subnetString)
{
    var subnetVals = subnetString.Split("/");
    if (subnetVals.Length != 2 || !int.TryParse(subnetVals[1], out _) || !System.Net.IPAddress.TryParse(subnetVals[0], out _))
    {
        return null;
    }

    #if true
    // Two queries, without join
    var subnet = await _appContext.Subnets.Where(s => s.SubnetCIDR == subnetVals[1] && s.FirstIpAddress == subnetVals[0]).FirstOrDefaultAsync();
    if (subnet == null)
    {
        return null;
    }
    var ips = await _appContext.IpAddresses.Where(ip => ip.SubnetId == subnet.Id).Select(ip => ip.IpAddressString).ToListAsync();
    /*
     info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (19ms) [Parameters=[@__p_0='16' (Size = 4000), @__p_1='192.168.1.0' (Size = 4000)], CommandType='Text', CommandTimeout='30']
      SELECT `s`.`Id`, `s`.`FirstIpAddress`, `s`.`SubnetCIDR`
      FROM `Subnets` AS `s`
      WHERE (`s`.`SubnetCIDR` = @__p_0) AND (`s`.`FirstIpAddress` = @__p_1)
      LIMIT 1
     info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (2ms) [Parameters=[@__subnet_Id_0='1'], CommandType='Text', CommandTimeout='30']
      SELECT `i`.`IpAddressString`
      FROM `IpAddresses` AS `i`
      WHERE `i`.`SubnetId` = @__subnet_Id_0
     */
    #endif
    #if false
    // One query with join
    var ips = await _appContext.IpAddresses
        .Where(ip => ip.Subnet.SubnetCIDR == subnetVals[1] && ip.Subnet.FirstIpAddress == subnetVals[0])
        .Select(ip => ip.IpAddressString)
        .ToListAsync();
    /*
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (161ms) [Parameters=[@__p_0='16' (Size = 4000), @__p_1='192.168.1.0' (Size = 4000)], CommandType='Text', CommandTimeout='30']
      SELECT `i`.`IpAddressString`
      FROM `IpAddresses` AS `i`
      INNER JOIN `Subnets` AS `s` ON `i`.`SubnetId` = `s`.`Id`
      WHERE (`s`.`SubnetCIDR` = @__p_0) AND (`s`.`FirstIpAddress` = @__p_1)
    */
    #endif
    return ips;
}