I have this method for searching a DB based on filters I was wondering if anyone thought as I do that this code is excessive, and secondly if you have any suggestions to shorten the code.
private const string NO_FILTER = "No filter";
[DataObjectMethod(DataObjectMethodType.Select)]
public static List<Part> GetPartsSearch(string partNumber, string modelNumber, string slotNumber, string yardNumber, int commodityId, string description)
{
using (var context = new EntitiesModel())
{
IQueryable<Part> parts;
if (slotNumber != NO_FILTER)
{
if (modelNumber != "0")
{
if (yardNumber != NO_FILTER)
{
if (commodityId > 0)
{
// Have slot, model, yard and commodity
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partSlot.SlotNumber == slotNumber
&& partModel.ModelNumber == modelNumber
&& partYard.YardNumber == yardNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
//Have slot, model and yard
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
where partSlot.SlotNumber == slotNumber
&& partModel.ModelNumber == modelNumber
&& partYard.YardNumber == yardNumber
select part;
}
}
else
{
//have slot, model and commodity
if (commodityId > 0)
{
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partSlot.SlotNumber == slotNumber
&& partModel.ModelNumber == modelNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
//Have slot and model
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
where partSlot.SlotNumber == slotNumber
&& partModel.ModelNumber == modelNumber
select part;
}
}
}
else if (yardNumber != NO_FILTER)
{
if (commodityId > 0)
{
//Have slot yard and commodity
parts = from part in context.Parts
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partSlot.SlotNumber == slotNumber
&& partYard.YardNumber == yardNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
//Have slot and yard
parts = from part in context.Parts
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
where partSlot.SlotNumber == slotNumber
&& partYard.YardNumber == yardNumber
select part;
}
}
else
{
if (commodityId > 0)
{
parts = from part in context.Parts
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partSlot.SlotNumber == slotNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
parts = from part in context.Parts
join partSlot in context.PartsBySlots on part.PartNumber equals
partSlot.PartNumber
where partSlot.SlotNumber == slotNumber
select part;
}
}
}
else if (modelNumber != "0")
{
if (yardNumber != NO_FILTER)
{
// Have model, yard and commodity
if (commodityId > 0)
{
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partModel.ModelNumber == modelNumber
&& partYard.YardNumber == yardNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
// Have model and yard
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
where partModel.ModelNumber == modelNumber
&& partYard.YardNumber == yardNumber
select part;
}
}
else
{
if (commodityId > 0)
{
// Have model and commodity
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partModel.ModelNumber == modelNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
// Have model
parts = from part in context.Parts
join partModel in context.PartsByModels on part.PartNumber equals
partModel.PartNumber
where partModel.ModelNumber == modelNumber
select part;
}
}
}
else if (yardNumber != NO_FILTER)
{
if (commodityId > 0)
{
//have yard and commodity
parts = from part in context.Parts
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where partYard.YardNumber == yardNumber
&& commodity.CommodityID == commodityId
select part;
}
else
{
// Have yard
parts = from part in context.Parts
join partYard in context.PartsByYards on part.PartNumber equals
partYard.PartNumber
where partYard.YardNumber == yardNumber
select part;
}
}
else if (commodityId > 0)
{
// Have commodity
parts = from part in context.Parts
join commodity in context.Commodities on part.CommodityId equals
commodity.CommodityID
where commodity.CommodityID == commodityId
select part;
}
else
{
parts = from part in context.Parts
select part;
}
if (partNumber != "0")
{
parts = parts.Where(p => p.PartNumber.ToLower().Contains(partNumber.ToLower()));
}
if(!string.IsNullOrEmpty(description))
{
parts = parts.Where(p => p.Description.ToLower().Contains(description.ToLower()));
}
parts = parts.OrderBy(p => p.PartNumber);
return parts.ToList();
}
}
ToUpper()
is faster thenToLower()
. And lets not forget that offloading this to the database in a SPROC or View would be much-much faster and a better use of resources overall. \$\endgroup\$