score:3

Accepted answer

since in you're example you're multiplying the fraction by 100, i'm assuming the subdept value would only have two digits. if it's the case, perhaps you could try the following code:

public ienumerable<inventoryitem> getdepartmentrange(double deptbegin, double deptend, string dbcontext)
{           
  loadinventoryitems(dbcontext);

  return inventoryitems
    .where(d => ((d.dept * 100 + d.subdept) >= deptbegin * 100) &&       
                ((d.dept * 100 + d.subdept) <= deptend * 100))
    .orderby(o => o.dept)
    .thenby(s => s.subdept);
}

score:2

a suggestion: create a function that takes in the deptnumber and deptsubnumber, and returns a decimal. then, use that function in-line in the .where() statement to process the numbers as a single number, rather than using separate logic (treating dept and sub-dept separately) .

score:2

an alternative suggestion: is it possible to create a calculated column on the database that simply combines the two fields together in the database, and returns it as a decimal?

then, use that function in-line in the .where() statement to process the numbers as a single number, rather than using separate logic (treating dept and sub-dept separately).

following this, all of the department/sub-department logic in the c# code would be moot.

score:3

your endfraction calculation is wrong:

public ienumerable<inventoryitem> getdepartmentrange(double deptbegin, double deptend, string dbcontext)
{
    loadinventoryitems(dbcontext);
    // break the doubles into their component parts:
    int deptstartwhole = (int)math.truncate(deptbegin);
    int startfraction = (int)((deptbegin - deptstartwhole) * 100);
    int deptendwhole = (int)math.truncate(deptend);
    int endfraction = (int)((deptend - deptendwhole) * 100);

    return inventoryitems
        .where(d => d.dept >= deptstartwhole)
        .where(e => e.subdept >= startfraction)
        .where(f => f.dept <= deptendwhole)
        .where(g => g.subdept >= endfraction)
        .orderby(o => o.dept)
        .thenby(s => s.subdept);
}

score:3

you are using a wrong variable in your calculation here:

var endfraction = (int)((deptbegin - deptendwhole) * 100);

replace deptbegin with deptend to fix this.

making this change appears to produce the desired values when tested with the following code:

public void showdeptrange(double deptbegin, double deptend)
{
    // break the doubles into their component parts:
    var deptstartw = (int)math.truncate(deptbegin);
    var deptstartf = (int)((deptbegin - deptstartw) * 100);
    var deptendw = (int)math.truncate(deptend);
    var deptendf = (int)((deptend - deptendw) * 100);

    console.writeline("{0}.{1}, {2}.{3}",
        deptstartw, deptstartf, deptendw, deptendf);
}

void main()
{
    showdeptrange(8.79, 98.87);
}

also see this answer, which addresses problems with your query's logic.

score:3

your condition is not correct. consider following:

int deptstartwhole = 8;
int startfraction = 79;
int deptendwhole = 98;
int endfraction = 87;

and your condition is:

d.dept >= deptstartwhole && d.subdept >= startfraction
&& d.dept <= deptendwhole && d.subdept <= endfraction

it will not return row with dept = 12 and subdept = 32, because 32 >= 79 (d.subdept >= startfraction check) is not true.

i think your condition should be

((d.dept == deptstartwhole && d.subdept >= startfraction) || d.dept > deptstartwhole)
&& ((d.dept == deptendwhole && d.subdept <= endfraction) || d.subdept < deptendwhole)

it checked subdept only when dept is exactly the same, and otherwise just check dept part, because that's the important one.

return inventoryitems
    .where(d => ((d.dept == deptstartwhole && d.subdept >= startfraction) || d.dept > deptstartwhole)
                && ((d.dept == deptendwhole && d.subdept <= endfraction) || d.subdept < deptendwhole))
    .orderby(o => o.dept)
    .thenby(s => s.subdept);

Related Query

More Query from same tag