Readdy Write

Volltext-Suche in Linq einbinden

13.07.2018 (👁8090)


Wie bindet man in Linq eine Abfrage nach Volltext ein?

Lösung:

Es ist sehr einfach die Volltext-Suche mit Linq und EF in C# Projekten einzubinden.

Man muss in der Abfrage nur .Conains(Suchwort) eingeben

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

 

Das tolle daran ist, dass dabei auch gleich auf Teilstrings ausgewertet wird, und man nicht schwierige Code-Strings zum Abfragen erzeugen muss.

Also bug in Debugger als Ergebnis.

Im Code Debugger

Beispiel:

Linq-Query Abfrage mit 3 Tabellen und der Suche nach linq in dem Feld: 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 >--

Ergebnisse in dem Beispiel

Ausgabe als Webseite mit Suchfilter

Kompletter C# Code in einer asp.net core 2 mvc anwendung

// 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 >------------- 

}


0,00 €