简体   繁体   English

LINQ,Lambda,C#,扩展方法

[英]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. 我几天来第一次玩linq到sql和lambda表达式,我想做以下几点。

I've got a string extension method that returns a double. 我有一个返回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. 我有一个使用linq到sql的表中列的字符串值列表,我想使用扩展方法作为过滤掉相似性得分等于或大于输入字符串的那些字符串的方法。

I've got the below so far. 到目前为止我已经得到了以下内容。 I don't seem to be able to test the value of the returned double. 我似乎无法测试返回的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. 距离方法是返回double的扩展方法。 Both Token and s are string. Token和s都是字符串。 ID is an integer ID field within a table. ID是表中的整数ID字段。

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. 比较5组日期时,最好不要错误地阅读标志。 The small one is on the left and the big one is on the right, 100% of the time. 小的一个在左边,大一个在右边,100%的时间。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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