EDN Admin
Well-known member
my question was how to sort list by the occurrence of word in every row of linq data....i got answer from here by someone. that is giving right output. here is the code<br/>
<br/>
void Main()<br/>
{<br/>
List<SearchResult> list = new List<SearchResult>() { <br/>
new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},<br/>
new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},<br/>
new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},<br/>
new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},<br/>
new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},<br/>
new SearchResult(){ID=6,Title="dog"},<br/>
};<br/>
<br/>
var to_search = new[] { "Geo", "JCB" };<br/>
<br/>
var result = from searchResult in list<br/>
let key_string = to_search.FirstOrDefault(ts => searchResult.Title.ToLower().Contains(ts.ToLower()))<br/>
group searchResult by key_string into Group<br/>
orderby Group.Count() descending<br/>
select Group;<br/>
result.ToList().Dump();<br/>
<br/>
<br/>
<br/>
}<br/>
// Define other methods and classes here<br/>
public class SearchResult<br/>
{<br/>
public int ID { get; set; }<br/>
public string Title { get; set; }<br/>
}<br/>
<br/>
i am getting the output like<br/>
<br/>
ID Title <br/>
-- ------<br/>
1 Geo Prism GEO 1995 GEO* - ABS #16213899 <br/>
3 Geo Prism GEO 1995 - ABS #16213899 <br/>
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO <br/>
2 Excavator JCB - ECU P/N: 728/35700 <br/>
4 JCB Excavator JCB- ECU P/N: 728/35700 <br/>
6 dog <br/>
<br/>
the above output is ok. all rows having the ord GEO comes first because it found maximum time in most of the rows means GEO the word found in 3 rows and JCB found in two rows so JCB related rows comes next.<br/>
<br/>
i need another sort after getting the above output on whole data....that is that GEO rows comes first which row has the GEO word maximum time...so my output would look like below<br/>
<br/>
ID Title <br/>
-- ------<br/>
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO <br/>
1 Geo Prism GEO 1995 GEO* - ABS #16213899 <br/>
3 Geo Prism GEO 1995 - ABS #16213899 <br/>
4 JCB Excavator JCB- ECU P/N: 728/35700 <br/>
2 Excavator JCB - ECU P/N: 728/35700 <br/>
6 dog <br/>
<br/>
i found a linq query which Count Occurrences of a Word in string<br/>
here is the code<br/>
<br/>
string text = @"Historically, the world of data and data the world of objects data" ;<br/>
string searchTerm = "data";<br/>
//Convert the string into an array of words<br/>
string[] source = text.Split(new char[] { ., ?, !, , ;, :, , }, StringSplitOptions.RemoveEmptyEntries);<br/>
var matchQuery = from word in source<br/>
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()<br/>
select word;<br/>
int wordCount = matchQuery.Count();<br/>
<br/>
i got it from this url http://msdn.microsoft.com/en-us/library/bb546166.aspx<br/>
<br/>
so tell me how could i use the above code to sort my title. how could use second sort for Count the Occurrences of a Word in title field as a result my output would look like<br/>
<br/>
ID Title <br/>
-- ------<br/>
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO <br/>
1 Geo Prism GEO 1995 GEO* - ABS #16213899 <br/>
3 Geo Prism GEO 1995 - ABS #16213899 <br/>
4 JCB Excavator JCB- ECU P/N: 728/35700 <br/>
2 Excavator JCB - ECU P/N: 728/35700 <br/>
6 dog
View the full article
<br/>
void Main()<br/>
{<br/>
List<SearchResult> list = new List<SearchResult>() { <br/>
new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},<br/>
new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},<br/>
new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},<br/>
new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},<br/>
new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},<br/>
new SearchResult(){ID=6,Title="dog"},<br/>
};<br/>
<br/>
var to_search = new[] { "Geo", "JCB" };<br/>
<br/>
var result = from searchResult in list<br/>
let key_string = to_search.FirstOrDefault(ts => searchResult.Title.ToLower().Contains(ts.ToLower()))<br/>
group searchResult by key_string into Group<br/>
orderby Group.Count() descending<br/>
select Group;<br/>
result.ToList().Dump();<br/>
<br/>
<br/>
<br/>
}<br/>
// Define other methods and classes here<br/>
public class SearchResult<br/>
{<br/>
public int ID { get; set; }<br/>
public string Title { get; set; }<br/>
}<br/>
<br/>
i am getting the output like<br/>
<br/>
ID Title <br/>
-- ------<br/>
1 Geo Prism GEO 1995 GEO* - ABS #16213899 <br/>
3 Geo Prism GEO 1995 - ABS #16213899 <br/>
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO <br/>
2 Excavator JCB - ECU P/N: 728/35700 <br/>
4 JCB Excavator JCB- ECU P/N: 728/35700 <br/>
6 dog <br/>
<br/>
the above output is ok. all rows having the ord GEO comes first because it found maximum time in most of the rows means GEO the word found in 3 rows and JCB found in two rows so JCB related rows comes next.<br/>
<br/>
i need another sort after getting the above output on whole data....that is that GEO rows comes first which row has the GEO word maximum time...so my output would look like below<br/>
<br/>
ID Title <br/>
-- ------<br/>
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO <br/>
1 Geo Prism GEO 1995 GEO* - ABS #16213899 <br/>
3 Geo Prism GEO 1995 - ABS #16213899 <br/>
4 JCB Excavator JCB- ECU P/N: 728/35700 <br/>
2 Excavator JCB - ECU P/N: 728/35700 <br/>
6 dog <br/>
<br/>
i found a linq query which Count Occurrences of a Word in string<br/>
here is the code<br/>
<br/>
string text = @"Historically, the world of data and data the world of objects data" ;<br/>
string searchTerm = "data";<br/>
//Convert the string into an array of words<br/>
string[] source = text.Split(new char[] { ., ?, !, , ;, :, , }, StringSplitOptions.RemoveEmptyEntries);<br/>
var matchQuery = from word in source<br/>
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()<br/>
select word;<br/>
int wordCount = matchQuery.Count();<br/>
<br/>
i got it from this url http://msdn.microsoft.com/en-us/library/bb546166.aspx<br/>
<br/>
so tell me how could i use the above code to sort my title. how could use second sort for Count the Occurrences of a Word in title field as a result my output would look like<br/>
<br/>
ID Title <br/>
-- ------<br/>
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO <br/>
1 Geo Prism GEO 1995 GEO* - ABS #16213899 <br/>
3 Geo Prism GEO 1995 - ABS #16213899 <br/>
4 JCB Excavator JCB- ECU P/N: 728/35700 <br/>
2 Excavator JCB - ECU P/N: 728/35700 <br/>
6 dog
View the full article