[英]linq ToArray<string>().Join(…) method is not working?
I have a datatble with one
column 我有
one
列数据
I want to concat each filename with wrapper div 我想用包装器div连接每个文件名
something like : 就像是 :
<div>File Attached : file1 </div>
<div>File Attached : file2 </div>
<div>File Attached : file3 </div>
...
I have succedded with : 我成功了:
string f = String.Join(" ", (from dr in dt.AsEnumerable()
select "<div>File Attached : " + dr[0].ToString() + "</div>").ToArray<string>());
but didnt success with : 但没有成功:
http://i.stack.imgur.com/xCKLD.jpg http://i.stack.imgur.com/xCKLD.jpg
How can i do it with the . 我该怎么办呢。
Join
method after the ToArray
method ? ToArray
方法之后的Join
方法?
Join
is not the same as String.Join
as you have discovered. Join
与您发现的String.Join
。 Enumerable.Join
works like joining in databases (but still on local objects, Queryable.Join works with SQL.) An example is given here: Enumerable.Join
就像加入数据库一样(但仍然在本地对象上,Queryable.Join与SQL一起工作。)这里给出一个例子:
http://msdn.microsoft.com/en-us/library/bb534675.aspx http://msdn.microsoft.com/en-us/library/bb534675.aspx
You could (but I don't recommend it because it will be ambiguous to the user) write an extension method like this: 你可以(但我不推荐它,因为它对用户来说是模棱两可的)写一个像这样的扩展方法:
public static class StringExtensions
{
public string Join(this IEnumerable<string> source, string sep)
{
return string.Join(sep, source.ToArray()); // You can erase `.ToArray()` if you're using .Net 4
}
}
and then it would work with (...).Join(" ")
. 然后它将与
(...).Join(" ")
。
My solution: (4.0) 我的解决方案:(4.0)
var query = from dr in dt.AsEnumerable() // You may be able to erase `AsEnumerable` depending on your source
select "<div>File Attached : " + dr[0] + "</div>";
var f = string.Join(" ", query);
My solution: (3.5) 我的解决方案:(3.5)
var arr = dt.AsEnumerable()
.Select(dr => "<div>File Attached : " + dr[0] + "</div>")
.ToArray();
var f = string.Join(" ", arr);
Are you sure that that the Join function is the correct one? 你确定Join功能是正确的吗? It sounds like you could be using the
Aggregate
function to end up with the same result, like this: 听起来您可能正在使用
Aggregate
函数来获得相同的结果,如下所示:
dt.AsEnumerable().Aggregate(string.Empty, (x,y) => x + string.Format("<div>File Attached : {0} </div>", y));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.