0

您将如何编写 MySpecialFunction 方法以伴随以下代码产生如下所示的输出?我想在逗号分隔的列表中显示前三个属性,然后计算剩余的项目并将它们报告为数字。我想用 lambda 表达式指定属性。感谢您的帮助!

public class MyObject
{
  public Text1 {get; set;}
  public Text2 {get; set;}

  public MyObject(string text1, string text2)
  {
    Text1 = text1;
    Text2 = text2;
  }
}

public class Main()
{
  List<MyObject> myObjects = new List<MyObject>();
  myObjects.Add(new MyObject("sample11", "sample12");
  myObjects.Add(new MyObject("sample21", "sample22");
  myObjects.Add(new MyObject("sample31", "sample32");
  myObjects.Add(new MyObject("sample41", "sample42");
  myObjects.Add(new MyObject("sample51", "sample52");

  MySpecialFunction(myObjects, f => f.Text1); 
  MySpecialFunction(myObjects, f => f.Text2);
}

输出字符串是:

样品 11、样品 21、样品 31 和另外 2 个。

样品 12、样品 22、样品 32 和另外 2 个。

谢谢!

4

4 回答 4

2

您可以使用类似下面的代码:

public class StackOverflow_14944788
{
    private static void MySpecialFunction(IEnumerable<MyObject> list, Func<MyObject, string> selector, int count = 3)
    {
        string result = string.Join(", ", list.Take(count).Select(selector));
        int listSize = list.Count();
        if (listSize > count)
        {
            result += ", and " + (listSize - count) + " more.";
        }

        Console.WriteLine(result);
    }

    public class MyObject
    {
        public string Text1 { get; set; }
        public string Text2 { get; set; }

        public MyObject(string text1, string text2)
        {
            Text1 = text1;
            Text2 = text2;
        }
    }

    public static void Test()
    {
        List<MyObject> myObjects = new List<MyObject>();
        myObjects.Add(new MyObject("sample11", "sample12"));
        myObjects.Add(new MyObject("sample21", "sample22"));
        myObjects.Add(new MyObject("sample31", "sample32"));
        myObjects.Add(new MyObject("sample41", "sample42"));
        myObjects.Add(new MyObject("sample51", "sample52"));

        MySpecialFunction(myObjects, f => f.Text1);
        MySpecialFunction(myObjects, f => f.Text2);
    }
}
于 2013-02-18T20:40:41.670 回答
1

这是一个通用版本,可以防止异常,并在您的集合中的项目少于maxCount时安全地处理这种情况。它还避免了多次枚举input

static string MySpecialFunction<T>(IEnumerable<T> input, Func<T, string> selector, int maxCount)
{
    if (input == null)
        throw new ArgumentNullException("input");

    if (selector == null)
        throw new ArgumentNullException("selector");

    if (maxCount <= 0)
        throw new ArgumentOutOfRangeException("maxCount must be greater than 0.");

    var items = input.Select(selector).ToArray();
    if (items.Length < 1)
    {
        return string.Empty;
    }
    else if (items.Length == 1)
    {
        return items[0];
    }
    else
    {
         var joined = String.Join(", ", items, 0, Math.Min(items.Length, maxCount));
         if (items.Length > maxCount)
         {
              joined += String.Format(", and {0} more.", items.Length - maxCount);
         }
         return joined;
    }
}

你会像这样使用它:

Console.WriteLine(MySpecialFunction(myObjects, o => o.Text1, 3));
于 2013-02-18T20:40:49.030 回答
0

像这样的东西(看到它运行):

static string MySpecialFunction(IEnumerable<string> items, int limit)
{
    var sb = new StringBuilder();

    bool delimit = false;
    int count = 0;

    foreach (var item in items) {
        if (limit == 0) {
            ++count;
        } else {
            if (delimit) {
                sb.Append(", ");
            }

            delimit = true;
            sb.Append(item);

            --limit;
        }
    }

    if (count != 0) {
        sb.Append(", and ");
        sb.Append(count);
        sb.Append(" more.");
    }

    return sb.ToString();
}

然后你会像这样执行它:

var summary = MySpecialFunction(myObjects.Select(i => i.Text1), 3);
于 2013-02-18T20:36:08.773 回答
0
private String MySpecialFunction<T>(IEnumerable<T> items, Func<T, String> selector)
{
    var prefix = items.Take(3).Select(selector);
    var suffix = " and " + (items.Count() - 3) +" more.";
    return prefix + suffix;
}
于 2013-02-18T20:39:46.990 回答