How can I fix this LINQ so that it considers pairs of shafts as a separate value?

The database I'm working in stores a pair of double values ​​in two integer columns.

The IOW department is double with values ​​such as 42.12, but is stored in the Dept column (which in this case is 42) and the Subdept column (which in this case contains 12). Do not ask me why (because you will be disappointed, I cannot tell you why, because I do not know).

I need to get a subset of data such as "all department records between 8.79 and 98.87"

I have this code:

public IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd, string dbContext)
{
    LoadInventoryItems(dbContext);
    // Break the doubles into their component parts:
    var deptStartWhole = (int)Math.Truncate(deptBegin);
    var startFraction = (int)((deptBegin - deptStartWhole) * 100);
    var deptEndWhole = (int)Math.Truncate(deptEnd);
    var endFraction = (int)((deptBegin - deptEndWhole) * 100);

    return inventoryItems
        .Where(d => d.dept >= deptStartWhole)
        .Where(e => e.subdept >= startFraction)
        .Where(f => f.dept <= deptEndWhole)
        .Where(g => g.subdept >= endFraction)
        //.Where(g => g.subdept <= endFraction)
        .OrderBy(o => o.dept)
        .ThenBy(s => s.subdept);
}

... but it didn’t quite work - with the above query (Depts between 8.79 and 98.87) it shows deblocks that are 98.88, which is one of what I want.

, ( ).

, "" , , .

UPDATE

enter image description here

+3
6

100, , subdept . , , :

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);
}
+3

endFraction :

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);
}
+3

:

var endFraction = (int)((deptBegin - deptEndWhole) * 100);

deptBegin deptEnd, .

:

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);
}

. , .

+3

. :

int deptStartWhole = 8;
int startFraction = 79;
int deptEndWhole = 98;
int endFraction = 87;

:

d.dept >= deptStartWhole && d.subdept >= startFraction
&& d.dept <= deptEndWhole && d.subdept <= endFraction

dept = 12 subdept = 32, 32 >= 79 (d.subdept >= startFraction check) true.

,

((d.dept == deptStartWhole && d.subdept >= startFraction) || d.dept > deptStartWhole)
&& ((d.dept == deptEndWhole && d.subdept <= endFraction) || d.subdept < deptEndWhole)

subdept , dept , dept , .

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);
+3

: , deptNumber deptSubNumber, a decimal. .Where(), , ( ).

+2

Alternative suggestion: is it possible to create a computed column in the database that simply combines the two fields together in the database and returns them as decimal?

Then use this function in a string in an expression .Where()to process numbers as a single number, and not to use separate logic (processing departments and subsections separately).

After that, the whole logic of the department / sub-department in C # code will be a moot point.

+2
source

All Articles