2
\$\begingroup\$

I have a few if, else-if, else clauses. The only different thing in the context of these clauses is my LINQ query, one clause has StartsWith, one clause has Contains, and the other does a simple match. Is there a way to have all of this in one clause and somehow create a variable that can change between StartsWith, Contains, etc.? My ultimate goal is to reduce the lines of code.

if (parameters[0].ToUpper() == "STARTSWITH" && parameters[1] != null)
    {
        string searchname = parameters[1];
        ViewBag.MyMessage = "Displaying device names beginning with \"" + searchname + "\"";

        SampleEntities sampleEntities = new SampleEntities();

        try
        {
            var Model = (from dev in sampleEntities.NetworkDevices
                         where dev.Name.StartsWith(searchname)
                         from inter in sampleEntities.DeviceInterfaces
                         where inter.NetworkDevice.Id == dev.Id
                         select new DeviceInterfaceModel
                         {
                             DeviceName = dev.Name,
                             InterfaceName = inter.Name,
                             IPv4Address = inter.IPv4Address,
                             IPv4SubnetMask = inter.IPv4SubnetMask,
                             CIDR = inter.CIDR,
                             Subnet = inter.Subnet
                         }).ToList();

            return View(Model);
        }
        catch
        {
        }

        return View();
    }
    #endregion
    #region Name Contains
    else if (parameters[0].ToUpper() == "CONTAINS" && parameters[1] != null)
    {
        string searchname = parameters[1];
        ViewBag.MyMessage = "Displaying device names containing \"" + searchname + "\"";

        SampleEntities sampleEntities = new SampleEntities();

        try
        {
            var Model = (from dev in sampleEntities.NetworkDevices
                         where dev.Name.Contains(searchname)
                         from inter in sampleEntities.DeviceInterfaces
                         where inter.NetworkDevice.Id == dev.Id
                         select new DeviceInterfaceModel
                         {
                             DeviceName = dev.Name,
                             InterfaceName = inter.Name,
                             IPv4Address = inter.IPv4Address,
                             IPv4SubnetMask = inter.IPv4SubnetMask,
                             CIDR = inter.CIDR,
                             Subnet = inter.Subnet
                         }).ToList();

            return View(Model);
        }
        catch
        {
        }

        return View();
    }
    #endregion
}

else
{
    ViewBag.MyMessage = "Diplaying device names equal to \"" + name + "\"";
    SampleEntities sampleEntities = new SampleEntities();

    try
    {
        var Model = (from dev in sampleEntities.NetworkDevices
                     where dev.Name == name
                     from inter in sampleEntities.DeviceInterfaces
                     where inter.NetworkDevice.Id == dev.Id
                     select new DeviceInterfaceModel
                     {
                         DeviceName = dev.Name,
                         InterfaceName = inter.Name,
                         IPv4Address = inter.IPv4Address,
                         IPv4SubnetMask = inter.IPv4SubnetMask,
                         CIDR = inter.CIDR,
                         Subnet = inter.Subnet
                     }).ToList();

        return View(Model);
    }
    catch
    {
    }

    return View();
}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

This should be more simplified and more clear, now we make the query to the devices context first, then we get the result, instead of repeating the same thing three times. Remember, always use using when dealing with objects that inherits IDisposable:

using (SampleEntities sampleEntities = new SampleEntities())
{
    string searchname = parameters[1];

    var devices = (from dev in sampleEntities.NetworkDevices
        select dev);

    if (parameters[0].ToUpper() == "STARTSWITH" 
        && parameters[1] != null)
    {
        ViewBag.MyMessage = "Displaying device names beginning with \"" + searchname + "\"";
        devices = (from dev in devices 
            where dev.Name.StartsWith(searchname));
    }
    else if (parameters[0].ToUpper() == "CONTAINS" 
        && parameters[1] != null)
    {
        ViewBag.MyMessage = "Displaying device names containing \"" + searchname + "\"";
        devices = (from dev in devices 
            where dev.Name.Contains(searchname));
    }   
    else
    {
        ViewBag.MyMessage = "Diplaying device names equal to \"" + name + "\"";
        devices = (from dev in devices 
            where dev.Name == name);
    }

    var Model = (from dev in devices
                 from inter in sampleEntities.DeviceInterfaces
                 where inter.NetworkDevice.Id == dev.Id
                 select new DeviceInterfaceModel
                 {
                     DeviceName = dev.Name,
                     InterfaceName = inter.Name,
                     IPv4Address = inter.IPv4Address,
                     IPv4SubnetMask = inter.IPv4SubnetMask,
                     CIDR = inter.CIDR,
                     Subnet = inter.Subnet
                 })
    .ToList();

    return View(Model);
}
\$\endgroup\$
3
  • \$\begingroup\$ Thank you, that works! I do have a concern with the performance. It seems like a database call is made the first time that gets all devices via the "var devices = (from dev in sampleEntities.NetworkDevices select dev);" statement. If theoretically there were millions of devices in this database, is there a better way to optimize the performance of this? \$\endgroup\$
    – LIK
    Commented Mar 1, 2014 at 3:15
  • \$\begingroup\$ @LIK no, it is just a query, no database call. The db call will occur when you call ToList() in the above code. \$\endgroup\$ Commented Mar 1, 2014 at 8:43
  • \$\begingroup\$ from dev in sampleEntities.NetworkDevices select dev There is no reason for this, just write sampleEntities.NetworkDevices instead (possibly specifying the type explicitly). from dev in devices where dev.Name == name This won't compile, you also need a select. If you have only one clause, I think that using the method syntax (.Where()) is better. \$\endgroup\$
    – svick
    Commented Mar 1, 2014 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.