2012-07-11 50 views
0

根據我的要求我設法得到從這個網站LINQ查詢,但是當我在整個事情粘貼LINQ墊它給錯誤的問題。在這裏,我要貼全LINQ查詢在那裏我得到錯誤消息像遇到regading LINQ和拉姆達

void Main() 
{ 
    List<SearchResult> list = new List<SearchResult>() { 
    new SearchResult(){ID=4,Title="Wie man BBA reman erreicht"}, 
    new SearchResult(){ID=5,Title="Ersatz Airbags, Gurtstrammer und Auto Körper Teile "}, 
    new SearchResult(){ID=6,Title="JCB Excavator - ECU P/N: 728/35700"}, 
    new SearchResult(){ID=2,Title="Geo Prism 1995 GEO - ABS #16213899"}, 
    new SearchResult(){ID=3,Title="Geo Prism 1995 - ABS #16213899"}, 
    new SearchResult(){ID=1,Title="Geo Prism 1995 GEO GEO- ABS #16213899"}, 
    }; 

var to_search = new[] { "Geo", "JCB" }; 
var result = (from searchResult in list 
      let title = searchResult.Title.ToLower() 
      let key_string = to_search.FirstOrDefault(ts => title.Contains(ts)) 
      orderby key_string == null ? -1 : title.Split(new[] { key_string }, StringSplitOptions.None).Length descending 
      group searchResult by key_string into Group 
      select Group).OrderByDescending(grp => grp.Count()).ThenByDescending(CountStringOccurrences(**grp.Key**, to_search)); 
} 

public int CountStringOccurrences(string text, string[] pattern) 
{ 
// Loop through all instances of the string 'text'. 
int count = 0; 
foreach (string itm in pattern) 
{ 
    int i = 0; 
    while ((i = text.IndexOf(itm, i)) != -1) 
    { 
     i += itm.Length; 
     count++; 
    } 
} 
return count; 
} 

public class SearchResult{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
} 

大膽區域拋出錯誤「名稱‘GRP’並不在當前的背景下存在」。感謝plzz已經看清楚。

回答

2

更改查詢結束從

ThenByDescending(CountStringOccurrences(grp.Key, to_search)); 

ThenByDescending(grp => CountStringOccurrences(grp.Key, to_search)); 

因爲Enumerable.ThenByDescending需要的KeySelectors功能,要通過searchresult -Group。