【问题标题】:Create linq extension method that takes a property and function and executes that function on that property创建 linq 扩展方法,该方法采用属性和函数并在该属性上执行该函数
【发布时间】:2020-02-14 07:31:16
【问题描述】:

正如标题所说,我正在尝试创建一个 linq 扩展方法,该方法允许我仅在一个对象属性上运行一个函数,不幸的是,我在这方面做得还不够远,而我真正拥有的是(我相信的)是一种方法的外壳:

 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();
    }

对于上下文,我有这个函数可以从字符串中删除所有 html 元素:

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 [] {"&nbsp;","&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;
    }

我正在使用 EF 获取数据,我需要为 4 个不同的表使用此函数(因此希望使其具有通用性)。所以如果我说这个类:

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;

^^ 被截断,因为它是一个 EF 类

我希望能够做到

dc.EmailTemplates
    .Where(x=>x.Body != null)
    .ChangeProperty(x=>x.Body, RemoveUnwantedTags)
    .ToList();

【问题讨论】:

  • 虽然上述内容并不难(应该花一个小时,然后您大致了解表达式),但您尝试归档的内容并不是 IQueryable 的工作方式,并且只会给您带来一件事:异常.
  • @X39 感谢您的回复,我刚刚开始更深入地了解泛型和表达式,并假设在进行 Linq 扩展时它需要是一个 IQueryable 方法。如果不使用 IQueryable,我将如何做到这一点?
  • 我猜我会改用 IEnumerable,因为 iQueryable 实现了它?
  • 你只使用 IEnumerable,正确。对于您的方法本身,请参阅例如。 stackoverflow.com/questions/2055927/… 因为您在这里并不需要任何表达式。只需在您的方法中重新分配值并在之后返回它
  • 我去看看,谢谢!

标签: c# linq extension-methods


【解决方案1】:

你可以在每个元素上调用t.Body = RemoveUnwantedTags(t.Body)。

dc.EmailTemplates
    .Where( t => t.Body != null)
    .ToList()//Load from db
    .ForEach( t => t.Body = RemoveUnwantedTags(t.Body)) //This is done in memory, not in the database

【讨论】:

  • 我试图用它来学习泛型和扩展方法,但我很欣赏这是一个干净简单的问题解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 2017-03-04
  • 1970-01-01
  • 2011-03-08
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多