开发者

linq subquery child collection to string

i am trying to figure out how to write a linq query that will return a child collections "name" property as a string.

I have a BO that has a "options" property where the options are the "name" property of each option in an "order" object.

I would like the result to look something like

order.id = 12312
order.date = 12/03/10
order.options = "Option 1 Name, Option 2 Name, Option 3 Name"

I hope this makes sense. thanks for any and a开发者_StackOverflow中文版ll help!


While Aggregate will work it has O(n2) performance. If you need better performance you can use string.Join. Unfortunately this method doesn't accept an IEnumerable<string> so you have to also use ToArray to get what you want:

string.Join(", ", options.Select(o => o.Name).ToArray())


You can use Aggregate:

options.Aggregate((current, next) => current + ", " + next);

Note that this generates a new string for each operation, so if your options list is long you are better off using an old school approach with StringBuilder

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜