在方法内使用 lambda 并与列表匹配

发布于 2024-11-09 08:10:27 字数 1272 浏览 3 评论 0原文

我有以下代码用于生成 jquery 图表的数据。然而它是相当重复的,为了保持 DRY 我想通过引入一种方法来重构。

sb.Append("{name: 'Pull Ups', data: [");
foreach(var item in data)
{
    sb.Append(prefix); prefix = ",";
    sb.Append(item.PullUps);
}
sb.Append("]}");

sb.Append("{name: 'Push Ups', data: [");
prefix = string.Empty;
foreach (var item in data)
{
    sb.Append(prefix);  prefix = ",";
    sb.Append(item.PushUps);
}
sb.Append("]}");

我需要的方法类似于

void Data(ref StringBuilder sb, string title, IList<DataDto> data,  ...)

我希望我的代码类似于:-

Data(ref sb, "Pull Ups", data, d=>d.PullUps);
Data(ref sb, "Push Ups", data, d=>d.PushUps);
Data(ref sb, "Squats", data, d=>d.Squats);

但是我正在努力找出如何做到这一点。我知道我需要使用类似的东西

private static void Data<T, TProp>(ref StringBuilder sb, 
   IList<T> data, Func<T, TProp> selector)

(pusedocode),

foreach(var item in data) {
  if (selector == ???)
   sb.Append(??);
}

我的 DataDto 非常简单:-

public class DataDto
{
    public virtual decimal PullUps { get;  set; }
    public virtual decimal PushUps { get;  set; }
    public virtual decimal Squats { get;  set; }
 ...
}

I have the following code that is used for producing data for a jquery graph. However it is quite repetitive and in order to keep DRY I would like to refactor by introducing a method.

sb.Append("{name: 'Pull Ups', data: [");
foreach(var item in data)
{
    sb.Append(prefix); prefix = ",";
    sb.Append(item.PullUps);
}
sb.Append("]}");

sb.Append("{name: 'Push Ups', data: [");
prefix = string.Empty;
foreach (var item in data)
{
    sb.Append(prefix);  prefix = ",";
    sb.Append(item.PushUps);
}
sb.Append("]}");

The method I need would be something like

void Data(ref StringBuilder sb, string title, IList<DataDto> data,  ...)

And I would love my code to be something like:-

Data(ref sb, "Pull Ups", data, d=>d.PullUps);
Data(ref sb, "Push Ups", data, d=>d.PushUps);
Data(ref sb, "Squats", data, d=>d.Squats);

However I am struggling to find out how to do this. I know I need to use somthing similar along the lines of

private static void Data<T, TProp>(ref StringBuilder sb, 
   IList<T> data, Func<T, TProp> selector)

and inside (pusedocode)

foreach(var item in data) {
  if (selector == ???)
   sb.Append(??);
}

My DataDto is quite simply:-

public class DataDto
{
    public virtual decimal PullUps { get;  set; }
    public virtual decimal PushUps { get;  set; }
    public virtual decimal Squats { get;  set; }
 ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-11-16 08:10:28

我想,你想要这样的东西:

void YourMethod<TItem, TProp>(StringBuilder sb, string name, IList<TItem> data,
                              Func<TItem, TProp> selector)
{
    var prefix = "";
    sb.Append("{name: '"+name+"', data: [");
    foreach(var item in data)
    {
        sb.Append(prefix);
        prefix = ",";
        sb.Append(selector(item).ToString());
    }
    sb.Append("]}");
}

你会这样称呼它:

YourMethod(sb, "Pull Ups", data, d=>d.PullUps);
YourMethod(sb, "Push Ups", data, d=>d.PushUps);
YourMethod(sb, "Squats", data, d=>d.Squats);

I think, you want something like this:

void YourMethod<TItem, TProp>(StringBuilder sb, string name, IList<TItem> data,
                              Func<TItem, TProp> selector)
{
    var prefix = "";
    sb.Append("{name: '"+name+"', data: [");
    foreach(var item in data)
    {
        sb.Append(prefix);
        prefix = ",";
        sb.Append(selector(item).ToString());
    }
    sb.Append("]}");
}

You would call it like this:

YourMethod(sb, "Pull Ups", data, d=>d.PullUps);
YourMethod(sb, "Push Ups", data, d=>d.PushUps);
YourMethod(sb, "Squats", data, d=>d.Squats);
怼怹恏 2024-11-16 08:10:28

简化现有的代码怎么样?

sb.Append("{name: 'Pull Ups', data: [");
sb.Append(String.Join(",", myExercises.Select(e=>e.PullUps));}
sb.Append("]}");

sb.Append("{name: 'Push Ups', data: [");
sb.Append(String.Join(",", myExercises.Select(e=>e.PushUps));}
sb.Append("]}");

另外,你可以做分段引体向上、俯卧撑或深蹲吗?小数是不是有点过分了?

What about just simplifying the code you have?

sb.Append("{name: 'Pull Ups', data: [");
sb.Append(String.Join(",", myExercises.Select(e=>e.PullUps));}
sb.Append("]}");

sb.Append("{name: 'Push Ups', data: [");
sb.Append(String.Join(",", myExercises.Select(e=>e.PushUps));}
sb.Append("]}");

Also, can you have fractional pull ups, push ups or squats? Is a decimal a bit overkill?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文