Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:11:14+00:00 2026-06-09T01:11:14+00:00

using LINQ i am query a data for sorting and i am getting right

  • 0

using LINQ i am query a data for sorting and i am getting right answer.

here is my data & linq query……please have look.

void Main()
{
List<SearchResult> list = new List<SearchResult>() {
    new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},
    new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},
    new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},
    new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},
    new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},
    new SearchResult(){ID=6,Title="dog"},
};

var to_search = new[] { "Geo", "JCB" }.Select(x => x.ToLower()).ToArray();

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
    orderby Group.Count() descending
select Group;

result.Dump();

}

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

enter image description here

my question is what sql query i need to write as a result i will get same output in sql server 2000.
suppose my data is stored in sql server table like this way

Table : MyTable
------------------
ID  Title
-----------
1   Geo Prism GEO 1995 GEO* - ABS #16213899
2   Excavator JCB - ECU P/N: 728/35700
3   Geo Prism GEO 1995 - ABS #16213899
4   JCB Excavator JCB- ECU P/N: 728/35700
5   Geo Prism GEO,GEO 1995 - ABS #16213899 GEO
6   Maruti gear box #ABS 4587

MY Edit

i fix some syntax issue in ur sql….just have look

CREATE FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))     
returns @temptable TABLE (items varchar(8000))       
as       
begin       
   declare @idx int       
    declare @slice varchar(8000)       

    select @idx = 1       
        if len(@String)<1 or @String is null  return       

   while @idx!= 0       
   begin       
       set @idx = charindex(@Delimiter,@String)       
       if @idx!=0       
           set @slice = left(@String,@idx - 1)       
       else       
          set @slice = @String       

       if(len(@slice)>0)  
           insert into @temptable(Items) values(@slice)       

       set @String = right(@String,len(@String) - @idx)       
       if len(@String) = 0 break       
   end   
return       
end

DECLARE @Sterm varchar(MAX) 
SET @Sterm ='GEO JCB'
;WITH SearchResult (rnum, title)
as 
(   
(select 1 as rnum,'Geo Prism GEO 1995 GEO* - ABS #16213899' as title)
union all
(select 2 as rnum,'Excavator JCB - ECU P/N: 728/35700' as title)
union all
(select 3 as rnum,'Geo Prism GEO 1995 - ABS #16213899' as title)
union all
(select 4 as rnum,'JCB Excavator JCB- ECU P/N: 728/35700' as title)
union all
(select 5 as rnum,'Geo Prism GEO,GEO 1995 - ABS #16213899 GEO' as title)
union all
(select 6 as rnum,'dog' as title)
) 

select rnum, title from SearchResult
join 
( select lower(Items) as term 
  from dbo.Split(@Sterm , ' ')
) as search_terms
on lower(SearchResult.title) like '%' + search_terms.term +'%'
order by 
search_terms.term,
(select count(*)
from dbo.Split(lower(SearchResult.title),' ')
where Items = search_terms.term
) desc 

but it is not full filling my requirement.

1) query should return search term and as well as those rows also at end which has no relation with search term.

2) second sort should be there like those rows will come first where search term found maximum time like GEO & JCB.

u miss second order by descending based on occurrence of search term. if u see the image very properly then u can see what kind of out put i was asking here. thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:11:16+00:00Added an answer on June 9, 2026 at 1:11 am

    You can’t do it with SQL alone. You will need a stored routine to split the title string into substrings, then SQL can count the substrings that match your search.

    CREATE FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))     
    returns @temptable TABLE (items varchar(8000))       
    as       
    begin       
      declare @idx int       
      declare @slice varchar(8000)       
    
      select @idx = 1       
        if len(@String)<1 or @String is null  return       
    
      while @idx!= 0       
      begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
           set @slice = left(@String,@idx - 1)       
        else       
           set @slice = @String       
    
        if(len(@slice)>0)  
          insert into @temptable(Items) values(@slice)       
    
          set @String = right(@String,len(@String) - @idx)       
          if len(@String) = 0 break       
        end   
      return       
    end
    
    DECLARE @Sterm varchar(MAX) 
    SET @Sterm ='GEO JCB'
    ;WITH SearchResult (rnum, title)
    as 
    (   
        (select 1 as rnum,'Geo Prism GEO 1995 GEO* - ABS #16213899' as title)
        union all
        (select 2 as rnum,'Excavator JCB - ECU P/N: 728/35700' as title)
        union all
        (select 3 as rnum,'Geo Prism GEO 1995 - ABS #16213899' as title)
        union all
        (select 4 as rnum,'JCB Excavator JCB- ECU P/N: 728/35700' as title)
        union all
        (select 5 as rnum,'Geo Prism GEO,GEO 1995 - ABS #16213899 GEO' as title)
        union all
        (select 6 as rnum,'dog' as title)
    ) 
    select term,rnum, title
      from SearchResult
      left join 
         ( select lower(Items) as term 
             from dbo.Split(@Sterm,' ')
      ) as search_terms
      on lower(SearchResult.title) like '%' + search_terms.term +'%'
    order by 
      isnull(search_terms.term,'ZZZZZZZZZZZZZ'),
      (select count(*)
         from dbo.Split(lower(SearchResult.title),search_terms.term)
      ) desc, 
      rnum desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on the documentation here: http://docs.composite.net/Data/AccessingDataWithCSharp/How-to-Query-Data-Using-LINQ#_How_to_Query I need to query data in a table
I'm using LINQ to query data. Consider a case where the user only wants
I'm using a LINQ query on a DataTable where a data field may be
I have a record that came from the follow linq query: using (var context
I have been using LINQ with compiled queries, basically passing into the compiled query
I'm using a LINQ query to translate data inside a DataTable object to be
Has anyone found/coded extension method that query data (using linq to sql ) in
I'm trying to pre-fetch some foreign key data using a linq query. A quick
I am using linq to nhibernate to query data from large table. I am
I am using Linq to SQl to query the data. When I write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.