HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Counting the number of points that have both a larger theta and a larger radius than a given point

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thetanumberthecountinglargerpointpointsthanbothradius

Problem

I don't like the fact that I am selecting all the points, even though I only care about the count of them.

Which of the from parts should appear first, if it even matters?

var count = (from type in ValidTypes()
    from point in GetData(type)
    where point.Radius > referencePoint.Radius
        && point.Theta > referencePoint.Theta
    select point).Count()


ValidTypes() returns a few types, about 5.

GetData(type) may return many points, possibly 100,000.

This query is counting the number of points that have both a larger theta and a larger radius than a given point.

Is there a way to write this faster or more readable?

Solution

First, your query is written fine. Its structure is not a concern; the order of the froms is clearly correct, the select is appropriate. Where you could look for improvement that could change the query (code and possibly performance) would be in the GetData method. If, for example, GetData is getting data from an external, queryable source, then you may want to offload the filtering to it rather than getting all of the data points and filtering it inside your application, particularly since you have so many points of data (potentially). So maybe your query could instead be

(from type in ValidTypes()    
 select GetData(type, referencePoint).Count())
.Sum();

// or same meaning, different phrasing

(from type in ValidTypes()
from point in GetData(type, referencePoint)
select point).Count();

Code Snippets

(from type in ValidTypes()    
 select GetData(type, referencePoint).Count())
.Sum();

// or same meaning, different phrasing

(from type in ValidTypes()
from point in GetData(type, referencePoint)
select point).Count();

Context

StackExchange Code Review Q#138, answer score: 9

Revisions (0)

No revisions yet.