zeencat
zeencat

Reputation: 590

LINQ, Lambda, C#, extension methods

I've only been playing with linq to sql and lambda expressions for the first time for a few days and I want to do the following.

I've got a string extension method that returns a double. The extension method tests two strings and returns a similarity score. I have a list of string values from a column in a table using linq to sql and I want to use the extension method as a way of filtering out the only those strings whose similarity score is equal to or greater than the input string.

I've got the below so far. I don't seem to be able to test the value of the returned double.

List<int> ids = dc.ErrorIndexTolerances
                  .Where(n => n.Token.Distance(s) => .85)
                  .Select(n => n.ID)
                  .ToList();

The Distance Method is the extension method that returns a double. Both Token and s are string. ID is an integer ID field within a table.

Does anyone have any tips?

Upvotes: 2

Views: 1085

Answers (3)

Amy B
Amy B

Reputation: 110071

Does anyone have any tips?

I have a tip... never use "greater than", only use "less than".

.Where(n => .85 <= n.Token.Distance(s))

I follow this rule mainly because of date logic. When comparing 5 sets of dates, it's good to never make the mistake of mis-reading the sign. The small one is on the left and the big one is on the right, 100% of the time.

.Where(acct => acct.CreateTime <= now
  && acct.StartTime <= order.OrderDate
  && order.FulfilledDate <= acct.EndTime)

Upvotes: 3

Steve
Steve

Reputation: 216263

Perhaps this should be

n.Token.Distance(s) >= .85) 

Just a typo :-)

Upvotes: 4

Guffa
Guffa

Reputation: 700192

The greater or equal operator is >=, not =>.

List<int> ids =
  dc.ErrorIndexTolerances.Where(n => n.Token.Distance(s) >= .85)
  .Select(n => n.ID).ToList();

Upvotes: 9

Related Questions