Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Include full-text search in Linq

13.07.2018 (👁4621)


 

 

How do you include a full text query in Linq?

Solution:

It is very easy to integrate the full text search with Linq and EF in C # projects.

You only have to enter .Conains (search word) in the query

where (project.HTML.Contains("linq"))

 

The great thing about it is that it also evaluates to substrings, and you do not have to generate difficult code strings for queries.

So bug in debugger as a result.

 

In the code debugger

 

Example:

Linq query query with 3 tables and search linq in the field: HTML

//--< Get Linq.Query >--

//*gets last 10 Projects with View_Sum

var query = (from project in _dbContext.tbl_Projects

             join u in _dbContext.tbl_Users on project.IDOwner equals u.IDUser into user_and_projects

 

             from un in user_and_projects.DefaultIfEmpty()

             join aspUser in _dbContext.Users on un.IDAspNetUser equals aspUser.Id into user_and_projects_and_aspUser

             from unAsp in user_and_projects_and_aspUser.DefaultIfEmpty()

             where (project.HTML.Contains("linq"))

             orderby project.IDProject descending

             select new { project, unAsp.UserName }).Take(50);

//--</ Get Linq.Query >--

 

 

Results in the example

Output as a website with search filter

 

 

Complete C # code in an asp.net core 2 mvc application

 

// GET: /Project Root 

public async Task<IActionResult> Index_All()

{

    ///-------------< Index_All >------------- 

    //--< Get User ID >--

    //internal referenz-Number for tracking in tables

    long IDCurrent_User = await UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

    //--</ Get User ID >--

 

 

    //--< Get Linq.Query >--

    //*gets last 10 Projects with View_Sum

    var query = (from project in _dbContext.tbl_Projects

                 join u in _dbContext.tbl_Users on project.IDOwner equals u.IDUser into user_and_projects

 

                 from un in user_and_projects.DefaultIfEmpty()

                 join aspUser in _dbContext.Users on un.IDAspNetUser equals aspUser.Id into user_and_projects_and_aspUser

                 from unAsp in user_and_projects_and_aspUser.DefaultIfEmpty()

                 where (project.HTML.Contains("linq"))

                 orderby project.IDProject descending

                 select new { project, unAsp.UserName }).Take(50);

    //--</ Get Linq.Query >--

 

    ////-< match_IDlist >-

    //string MatchList = "";

    //foreach (var row in query)

    //{

    //    MatchList = MatchList + "-" + row.project.IDProject;

    //}

    //ViewData["MatchList"] = MatchList;

    ////-</ match_IDlist >-

 

    //----< fill Data_to_View >----

    List<Projects_Index_DataModel> dataList = new List<Projects_Index_DataModel>();

 

    //---< @Loop: Rows >---

    foreach (var row in query)

    {

        //--< Row to Data >--

        //< correct >

        string sShort = row.project.Text;

        if (sShort.Length > 255) { sShort = sShort.Substring(0, 255); }

 

        row.project.Text = sShort;

        //</ correct >

 

        //< Data >

        Projects_Index_DataModel item = new Projects_Index_DataModel();

        item.Project = row.project;

        item.Ownername = row.UserName;

        //</ Data >

 

        //< add >

        dataList.Add(item);

        //</ add >

        //--</ Row to Data >--

    }

    //---</ @Loop: Rows >---

    //----</ fill Data_to_View >----

 

    //< data to view >

    Projects_Index_View_DataModel dataView = new Projects_Index_View_DataModel();

    dataView.IDCurrent_User = IDCurrent_User;

    dataView.List_Projects_with_Owner = dataList;

    //</ data to view >

 

    //--< MetaData >--

    string sText = "";

    string sImage = "";

    if (query.Count() > 0)

    {

        var qResult= await query.FirstOrDefaultAsync(); 

        sText = qResult.project.Title;                

    }

    else

    {

        sText = "📜 latest news ";                

    }

    int lenMax200 = sText.Length;

    if (lenMax200 > 200) lenMax200 = 200;

    string sMetaDescription = sText.Substring(0, lenMax200);

    string sFacebook_AppID = Website_Constants.fp_appID;

    

    String sMetaData = "<meta property=\"og:title\" content=\"" + sText + "\" />";

    sMetaData += Environment.NewLine + "<meta property=\"og:type\" content=\"website\" />";

    sMetaData += Environment.NewLine + "<meta property=\"og:url\" content=\"" + Website_Constants.URL_Domain_Base + "/📜\" />";

    sMetaData += Environment.NewLine + "<meta property=\"og:site_name\" content=\"" + Website_Constants.Domain_Shortname + "\" />";

    sMetaData += Environment.NewLine + "<meta property=\"og:description\" content=\"" + sMetaDescription + "\">";

    sMetaData += Environment.NewLine + "<meta property=\"fb:app_id\" content=\"" + sFacebook_AppID + "\">";

    sMetaData += Environment.NewLine + "<meta property=\"og:image\" content=\"" + sImage + "\" />";

    

    sMetaData = System.Net.WebUtility.HtmlDecode(sMetaData);

    ViewData["MetaData"] = sMetaData;

    //--</ MetaData >--

 

    //< out to view >

    string sMobile_Extenstion = Request_Methods.set_optional_Mobile_Extension(Request);

    return View("index_all" + sMobile_Extenstion, dataView);

    //</ out to view >

    ///-------------</ Index_All >------------- 

}