score:1
I think there is no easy answer in your case, but you have several possibilities. Regex can be a simple solution if you do not want to get the full tags. A more deeper but more complex would be to use a package like HtmlAgilityPack to parse your mail.
Here is an example of the regex :
var searchWord = "strong";
var mail = "<strong>blablabla</strong><p>blabla strong blabla</p>";
var rgx = new Regex($"(?!<){searchWord}(?!>)"); // Will match strong but not <strong> or <strong or strong>
if (rgx.IsMatch(mail))
{
// Do what you want
}
score:-1
You can use the regex function as am using below:
"<div>Hello</div><span>World</span>".replace(/<[^>]*>/g, '')
score:1
See if you can use free and open source HtmlAgilityPack through which you can first convert the html text to plain text and then apply the search criteria.
E.g.: var plainTextResult = HtmlUtilities.ConvertToPlainText(string html);
if(!string.IsNullOrWhiteSpace(searchText))
{
bool containsResult = plainTextResult.Contains(searchText);
}
score:1
Thanks to @Amine and @lollmbaowtfidgafgtfoohwtbs, I figured out how to do this.
First, I created a SQL function in my database that strips a given text:
CREATE FUNCTION [dbo].[ufnStripHTML] (@HTMLText NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
WHILE @Start > 0 AND @End > 0 AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
END
RETURN LTRIM(RTRIM(@HTMLText))
END
GO
Then I added a reference to that function in my DbContext
:
[DbFunction("ufnStripHTML")]
public static string StripHTML(string text)
{
throw new Exception("not implemented");
}
and now I can use it in my Linq to SQL
query:
if (!string.IsNullOrWhiteSpace(searchText))
{
query = query.Where(ent => TGDbContext.StripHTML(ent.Contents).Contains(searchText));
}
Source: stackoverflow.com
Related Articles
- Searching for a text in a string property where the value is HTML
- Write html anchor text (with name / value pair) to a string
- List or Array of String Contain specific word in Html Source Code
- C# - code to order by a property using the property name as a string
- LINQ syntax where string value is not null or empty
- Linq filter List<string> where it contains a string value from another List<string>
- linq remove item from object array where property equals value
- LINQ to SQL - select where text like string array
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- Searching in text files for a keyword until a string is encountered
- LINQ to EF - Find records where string property of a child collection at least partially matches all records in a list of strings
- LINQ, where third nested children's property value is equal to something?
- Add " | " in string if where string value is not null or empty
- Searching in text files until specific string
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Filter IEnumerable<object> based on whether string property contains any string value of another List<string>
- Searching by fragments if values are string type but search by and entire if value is int
- C# Linq to XML getting the elements where value of name contains a specific string
- c# Lambda Expression - Get property value from string
- Generic predicate to implement searching by any string property
- Find XmlNode where attribute value is contained in string
- How to dynamically get a property value in which is in another property with Linq where statement
- Generically find any items where item property value is equal using LINQ
- Select where model property contains all in list of key value pairs
- Is there a Linq operation to retrieve specific items from a list of items where the item has a property value for a property which should be unique?
- Searching for an item in a list that's nested inside another list based on a property value in C# using LINQ?
- LINQ Compare two lists where property value is not equal
- How to remove a value from a specific property in a JSON string by matching value?
- How to find all items in a list where a property has a specific value or where there is a related item with this value?
- Linq extension. Change property value in source list
- How can I use LINQ and C# to find the oldest date from 3 different files?
- Select latest, distinct item with LINQ
- Multiple rows update without select
- Passing an implicit typed variable: "var" to a function in C# and returning an array?
- How to flatten object properties using linq
- Linq query with subquery as comma-separated values
- Is it possible two compare two fields in same IEnumerable using Linq?
- LINQ, entity that implements Interface and exception in mapping
- select A where A < B in LINQ
- How to detect if a LINQ enumeration is materialized?
- Building up linq querys as functions
- DataReader already opened and must be closed. I don't understand
- DataSet does not support System.Nullable<> error on linq
- Can't get Dynamic Linq to work
- Compare two complex list objects
- Filter data from 2 lists with diferent models C#
- Find the equal sets of strings and their count (distinct lists)
- How can I call a local function inside a LINQ query?
- Linq Any() times out
- How to use OrderBy in Linq