locked
Keyword search in linq. RRS feed

  • Question

  • User34108037 posted

    I have blog table. Blog search in my website. User will give keywords as comma separated values. eg. keyword1,keyword2,keyword3 etc..

    I want to search blog using this keywords. ie i will get the blogs list those title contains any one of those keywords. How will i do this linq?

    Suresh.

    Monday, February 13, 2012 11:49 PM

Answers

  • User1884951399 posted

        // This is the string to search within
        string userName = "Brian, Steen, Pedersen";
        // This is the search string
        string searchString = "Brian,Pedersen";
        // I split the search string into indiviual words
        string[] searchTerms = seachString.Split(',');
        // ... and return TRUE if any of the words is found inside the userName string
        bool found = searchTerms.Any(searchTerm => userName.ToLower().Contains(searchTerm.ToLower()));

    please mark as answwer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 13, 2012 11:59 PM
  • User-2146987983 posted

    Some sample code...to get the idea:

    //Test Class
    public class Blog
    {
        public int BId { get; set; }
        public string Tag { get; set; }
    }
    
    //Test Data
    protected List<Blog> GetBlogs()
    {
            return new List<Blog> { 
                new Blog { BId= 1, Tag = "A"}
                ,new Blog { BId= 2, Tag = "B"}
                ,new Blog { BId= 3, Tag = "C"}
                ,new Blog { BId= 4, Tag = "D"}
            };
    }
    
    //Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
                string searchText = "A,C,D";
                string[] splitArray = searchText.Split(new char[] { ',' });
                var v = from b in GetBlogs()
                        where splitArray.Contains(b.Tag)
                        select b;
    
                grid.DataSource = v.ToList();
                grid.DataBind();
            }
    }
    
    Place a gridview named "grid" on aspx and test the code..
    
    Hope it helps....
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 12:25 AM

All replies

  • User1884951399 posted

        // This is the string to search within
        string userName = "Brian, Steen, Pedersen";
        // This is the search string
        string searchString = "Brian,Pedersen";
        // I split the search string into indiviual words
        string[] searchTerms = seachString.Split(',');
        // ... and return TRUE if any of the words is found inside the userName string
        bool found = searchTerms.Any(searchTerm => userName.ToLower().Contains(searchTerm.ToLower()));

    please mark as answwer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 13, 2012 11:59 PM
  • User-2146987983 posted

    Some sample code...to get the idea:

    //Test Class
    public class Blog
    {
        public int BId { get; set; }
        public string Tag { get; set; }
    }
    
    //Test Data
    protected List<Blog> GetBlogs()
    {
            return new List<Blog> { 
                new Blog { BId= 1, Tag = "A"}
                ,new Blog { BId= 2, Tag = "B"}
                ,new Blog { BId= 3, Tag = "C"}
                ,new Blog { BId= 4, Tag = "D"}
            };
    }
    
    //Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
                string searchText = "A,C,D";
                string[] splitArray = searchText.Split(new char[] { ',' });
                var v = from b in GetBlogs()
                        where splitArray.Contains(b.Tag)
                        select b;
    
                grid.DataSource = v.ToList();
                grid.DataBind();
            }
    }
    
    Place a gridview named "grid" on aspx and test the code..
    
    Hope it helps....
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 12:25 AM
  • User1492915735 posted

    Hi,

    Don't forget to force it Tolower();

    e.g

    1. var search = from db in context.Entity.Field.Contain(factor).Tolower();

    2. var search = from db in context.Entity.Field.StartWith(factor).Tolower();

    Tuesday, February 14, 2012 12:33 AM