简体   繁体   中英

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?

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

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

Perhaps this should be

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

Just a typo :-)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM