C# 是否有等效于 string.Join(string, string[]) 的 LINQ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/934635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a LINQ equivalent of string.Join(string, string[])
提问by Matthew Dresser
Is there any way to convert a collection of objects into a single new object using LINQ?
有没有办法使用 LINQ 将一组对象转换为单个新对象?
I want to use this within another LINQ to SQL expression.
我想在另一个 LINQ to SQL 表达式中使用它。
采纳答案by bruno conde
Why don't you use the string.Join
itself?
你为什么不使用它string.Join
本身?
string.Join("<br/>", collection.Select(e => e.TextProp).ToArray());
回答by Jon Skeet
The normal way would be to use one of the aggregation operators (Aggregate
, Sum
, Average
etc), but it entirely depends on the type and what you want to do. What type are you interested in?
正常的方法是使用聚合运营商(之一Aggregate
,Sum
,Average
等),但它完全取决于类型和你想要做什么。你对什么类型感兴趣?
EDIT: Okay, so you want to concatenate strings... I don't thinkthere's anything which will do that in LINQ to SQL itself. Options:
编辑:好的,所以你想连接字符串......我认为在 LINQ to SQL 本身中没有任何东西可以做到这一点。选项:
- Write a stored proc or TVF to do it in SQL
- Fetch the individual strings in LINQ to SQL and concatenate back on the client side
- 编写存储过程或 TVF 以在 SQL 中执行此操作
- 在 LINQ to SQL 中获取单个字符串并在客户端连接回
回答by Scott Ivey
You can use the Aggregate method...
您可以使用聚合方法...
var myResults = (from myString in MyStrings
select myString)
.Aggregate(string.Empty, (results, nextString)
=> string.Format("{0}<br />{1}", results, nextString));
or
或者
var myResults = MyStrings.Aggregate(string.Empty, (results, nextString)
=> string.Format("{0}<br />{1}", results, nextString));
回答by Damian Powell
Most of the solutions here are fairly inefficient if you have large numbers of values you want to concatonate. Also, they're not all that readable. If you are going to do this sort of thing frequently, then it's worth building your own extension method to do it. The implementation below allows you to do the equivalent of string.Join(", ", arrayOfStrings)
where the arrayOfStrings can be an IEnumerable<T>
, and separator can be any object at all. It allows you to do something like this:
如果您有大量要连接的值,这里的大多数解决方案都相当低效。此外,它们并不是那么易读。如果你要经常做这种事情,那么构建你自己的扩展方法来做到这一点是值得的。下面的实现允许你做相当于string.Join(", ", arrayOfStrings)
arrayOfStrings 可以是 an 的地方IEnumerable<T>
,而分隔符可以是任何对象。它允许你做这样的事情:
var names = new [] { "Fred", "Barney", "Wilma", "Betty" };
var list = names
.Where(n => n.Contains("e"))
.Join(", ");
Two things I like about this are:
我喜欢这个的两件事是:
- It's very readable in a LINQ context.
- It's fairly efficient because it uses StringBuilder and avoids evaluating the enumeration twice which is important in a database scenario (L2S, L2E, or L2Nh).
- 它在 LINQ 上下文中非常易读。
- 它相当有效,因为它使用 StringBuilder 并避免对枚举进行两次评估,这在数据库场景(L2S、L2E 或 L2Nh)中很重要。
public static string Join<TItem,TSep>(
this IEnumerable<TItem> enuml,
TSep separator)
{
if (null == enuml) return string.Empty;
var sb = new StringBuilder();
using (var enumr = enuml.GetEnumerator())
{
if (null != enumr && enumr.MoveNext())
{
sb.Append(enumr.Current);
while (enumr.MoveNext())
{
sb.Append(separator).Append(enumr.Current);
}
}
}
return sb.ToString();
}