As the title says I'm trying to create a linq extension method that allows me to run a function on just one of an objects properties, unfortunately I've not made it very far on this and all I really have is (what I believe) is the shell of a method to do this:
public static IQueryable<T> ChangeProperty<T>(this IQueryable<T> source, Expression<Func<T, string>> selector, Func<T,T> method)
{
//Placeholder
return Enumerable.Empty<T>().AsQueryable();
}
For context, I have this function which strips all html elements out of a string:
public static string RemoveUnwantedTags(string data)
{
if (string.IsNullOrEmpty(data)) return string.Empty;
var document = new HtmlDocument();
document.LoadHtml(data);
var acceptableTags = new string[] { "strong", "em", "u" };
var nodes = new Queue<HtmlNode>(document.DocumentNode.SelectNodes("./*|./text()"));
while (nodes.Count > 0)
{
var node = nodes.Dequeue();
var parentNode = node.ParentNode;
if (acceptableTags.Contains(node.Name) || node.Name == "#text") continue;
var childNodes = node.SelectNodes("./*|./text()");
if (childNodes != null)
{
foreach (var child in childNodes)
{
nodes.Enqueue(child);
parentNode.InsertBefore(child, node);
}
}
parentNode.RemoveChild(node);
}
var htmlFormattingTags = new [] {" ","&tbsp;"};
if (!htmlFormattingTags.Any(x=>document.DocumentNode.InnerText.Contains(x))) return document.DocumentNode.InnerHtml;
var template = document.DocumentNode.InnerText;
foreach (var tag in htmlFormattingTags)
{
template = template.Replace(tag, " ");
}
return template;
}
I'm grabbing data using EF, and I'll need this function for 4 different tables (hence wanting to make it generic). So if I have say this class:
public partial class EmailTemplate : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _ID;
private string _tenant;
private string _TemplateName;
private bool _HTML;
private string _Body;
private string _Images;
private string _Subject;
private System.Nullable<bool> _SMS;
private System.Guid _rowguid;
private System.Nullable<int> _OverrideServer;
^^ Truncated because it's an EF class
I want to be able to do
dc.EmailTemplates
.Where(x=>x.Body != null)
.ChangeProperty(x=>x.Body, RemoveUnwantedTags)
.ToList();