如何编写方法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个。
sample12,sample22,sample32和另外2个。
谢谢!
答案 0 :(得分: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);
}
}
答案 1 :(得分: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));
答案 2 :(得分:0)
像这样(see it run):
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);
答案 3 :(得分: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;
}