C#列表<;类>;使用lambda表达式字符串的前3项
本文关键字:表达式 字符串 3项 lambda gt 列表 lt 使用 | 更新日期: 2023-09-27 17:59:51
您将如何编写方法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);
}
输出字符串为:
sample11、sample21、sample31和2个以上。
sample12、sample22、sample32和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);
}
}
这里有一个通用版本,它可以防止异常,并在集合中的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));
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;
}
类似的东西(请参阅运行):
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);