score:3
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);
Source: stackoverflow.com
Related Query
- How can I fix this LINQ so that it treats a pairs of vals as a distinct value?
- How can i fix this method using Linq so that the test (which should already work because the result is correct) works?
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- How can I combine this code into one or two LINQ queries?
- How can I further simplify this piece of LINQ code
- How can i fix this error? c# asp linq
- How can I modify this C# code so that Visual Studio recognizes that I'm not an idiot?
- This LINQ statement crashes if one of the Properties is NULL. How can I fix this?
- How can I fix this LINQ function?
- How can I refactor this code for LINQ filtering?
- How can I sort distinct vals in LINQ (C#)?
- How can I simplify this LINQ query that searches for keywords in strings and orders them by relevance?
- How can I simplify this LINQ code
- How to use Distinct option to filter the value of a Column Name that is related to different foreign Keys. C# .NET Lambda LINQ
- How can I return a single value integer from Linq from an array of objects that have other nested values
- How can i convert this code snippet into LINQ format?
- How can I fix the performance issue of this linq query?
- C# LINQ threw this error. how can I fix it?
- I have a LINQ statement that is partially query and partially method. How can I do this using one or the other?
- How can I do this LINQ query on a condition that something not exist in a table?
- How to write SQL translateable linq code that groups by one property and returns distinct list
- How can I get LINQ to return the object which has the max value for a given property?
- How can I make sure that FirstOrDefault<KeyValuePair> has returned a value
- How are people unit testing code that uses Linq to SQL
- How can I implement NotOfType<T> in LINQ that has a nice calling syntax?
- How to get distinct with highest value using Linq
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python?
More Query from same tag
- Empty namespace using Linq Xml
- Comparing record types with LINQ in C#
- Filter large list based on date time
- How to add left outer join to grouped and summed LINQ query
- PgAdmin Import/Export did not keep foreign keys with Entity Framework
- How to use LINQ to query for specific object in an array in a list of objects?
- Generating efficient LEFT JOIN with COUNT in Linq to Entities
- How do I get distinct rows along with the count of identical rows in Linq?
- how to print the innertext of an element based on attribute search of a particular node using LINQ?
- LINQ orderby string with integer where 1,11,12,13 are not next to eachother?
- LINQ to Object Join operator and equality
- What is the best pattern to use LINQ to C#
- Pull field from another table in MVC
- LINQ query compary multiple with multiple
- C# WinForm Excel check empty sheets function-works but too slow(15 sheets 30 seconds)
- How do you resolve ambiguity between types in Linq?
- Linq how to select only a small subset of a large result set?
- How to improve this query performance in Linq?
- LINQ Select expression IEnumerable
- CRM 2011 - Compare years in LINQ Query - Gives an error : Invalid 'where' condition. An entity member is invoking an invalid property or method
- Using c# to fill a variable with a LINQ any match
- Linq All Vs Foreach
- MVC 4. and Entity Framework Table Join
- How can I make a LINQ expression return into a class?
- How to find recursive Parent Child hierarchy Dynamically using Linq in Mvc
- Insert All only inserts last record? LINQ to SQL
- Sum of TimeSpan in Entity Framework Core
- Session variable returning null
- get commmon properties of two different object using c#
- C# LINQ group and count questions